ActionListener se llama varias veces (¿error?) - Mojarra 2.1.3

Tengo el siguiente botón:

     value="Search" />

When I debug I see that the actionListener is called twice first, then three times, next click four times and so on.

It seems like on every reload the actionListener is registered one more time.

I'm using Mojarra 2.1.3 (also tried 2.0.6) and Tomcat 7 with IceFaces.

The binding is done that way:

private javax.faces.component.UICommand searchButton;

public void setSearchButton(UICommand searchButton) {
  this.searchButton = searchButton;

public UICommand getSearchButton() {
  return searchButton;

preguntado el 08 de noviembre de 11 a las 11:11

How is the binding done? Can you post get/setSearchButton with all related code? -

The problem goes away when I remove the binding. @mrembisz: I added the binding code (sorry it's not formatted because I get a virus warning on the page preventing the editor to load). -

When you specify binding, your component will be reused if holding bean remains in scope. There can be many actionListeners on a single component, so with each request a new listener was registered with your command. -

2 Respuestas

That can happen if you've bound the component to a session or application scoped bean instead of a request scoped bean. This is simply a bad design. The very same component would be reused among multiple requests/views. You need to put the bean in the request scope, or to get rid of the component binding altogether.

Note that binding the component directly to a bean is often a sign of poor design somewhere in the code. What is it, the functional requirement and/or problem for which you thought that this is the solution? If you elaborate on that, we may be able to propose the right approach.

Also note that using an action listener alone is also a design smell. I'd expect "searchForLicenses" to be a normal action method. See also Diferencias entre action y actionListener.

contestado el 23 de mayo de 17 a las 13:05

I guess you're right. Still it's not very obvious for me that you can't use a session scoped bean in combination with component binding and actionListener. Why it isn't just checked if the listener has already been added to the component? Probably I'm thinking in the wrong direction here but as it's perfectly possible syntax-wise it shouldn't end up in such a mess... imho at least. - hugri

The Java or IDE compiler can't prevent "bad designs". It can at highest prevent syntax or logic errors. - BalusC

Fully agreed, still it's not obvious that it's working that way (to me). Also, when I'm using view scope, don't we end up in the same mess because on every reload the listeners are re-added since the beans are held as long as the users stays on the same page ie. view, right? - hugri

No, listeners aren't re-added because the component will be re-created. This is by the way in turn caused by a chicken-egg bug in Mojarra's partial view state saving. - BalusC

I'm confused :) correct behaviour would be to re-add listeners but it's not done that way because of a Mojarra bug? - hugri

The similar issue takes place when component is using binding y validator or valueChangListener and backing bean is of View, Session or Application scope. Then corresponding listeners are called many times but not once during request (+1 time with every new request).

One possible solution is to override jsf class AttachedObjectListHolder which is used for storing component listeners. Current implementation simply add new listener to component even though the same listener is already there. So the proposed fix is to check that listener does not exist before adding it.

Details of the fix you can see aquí

contestado el 23 de mayo de 17 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.