By Danijel


2012-08-09 08:05:17 8 Comments

What's the difference between the following two pieces of code - with regards to listener placement?

<h:selectOneMenu ...>
    <f:selectItems ... />
    <f:ajax listener="#{bean.listener}" />
</h:selectOneMenu>

and

<h:selectOneMenu ... valueChangeListener="#{bean.listener}">
    <f:selectItems ... />
</h:selectOneMenu>

2 comments

@BalusC 2012-08-09 12:02:13

The valueChangeListener will only be invoked when the form is submitted and the submitted value is different from the initial value. It's thus not invoked when only the HTML DOM change event is fired. If you would like to submit the form during the HTML DOM change event, then you'd need to add another <f:ajax/> without a listener(!) to the input component. It will cause a form submit which processes only the current component (as in execute="@this").

<h:selectOneMenu value="#{bean.value}" valueChangeListener="#{bean.changeListener}">
    <f:selectItems ... />
    <f:ajax />
</h:selectOneMenu>

When using <f:ajax listener> instead of valueChangeListener, it would by default executed during the HTML DOM change event already. Inside UICommand components and input components representing a checkbox or radiobutton, it would be by default executed during the HTML DOM click event only.

<h:selectOneMenu value="#{bean.value}">
    <f:selectItems ... />
    <f:ajax listener="#{bean.ajaxListener}" />
</h:selectOneMenu>

Another major difference is that the valueChangeListener method is invoked during the end of the PROCESS_VALIDATIONS phase. At that moment, the submitted value is not been updated in the model yet. So you cannot get it by just accessing the bean property which is bound to the input component's value. You need to get it by ValueChangeEvent#getNewValue(). The old value is by the way also available by ValueChangeEvent#getOldValue().

public void changeListener(ValueChangeEvent event) {
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();
    // ...
}

The <f:ajax listener> method is invoked during INVOKE_APPLICATION phase. At that moment, the submitted value is already been updated in the model. You can just get it by directly accessing the bean property which is bound to the input component's value.

private Object value; // +getter+setter.

public void ajaxListener(AjaxBehaviorEvent event) {
    System.out.println(value); // Look, (new) value is already set.
}

Also, if you would need to update another property based on the submitted value, then it would fail when you're using valueChangeListener as the updated property can be overridden by the submitted value during the subsequent UPDATE_MODEL_VALUES phase. That's exactly why you see in old JSF 1.x applications/tutorials/resources that a valueChangeListener is in such construct been used in combination with immediate="true" and FacesContext#renderResponse() to prevent that from happening. After all, using the valueChangeListener to execute business actions has actually always been a hack/workaround.

Summarized: Use the valueChangeListener only if you need to intercept on the actual value change itself. I.e. you're actually interested in both the old and the new value (e.g. to log them).

public void changeListener(ValueChangeEvent event) {
    changeLogger.log(event.getOldValue(), event.getNewValue());
}

Use the <f:ajax listener> only if you need to execute a business action on the newly changed value. I.e. you're actually interested in only the new value (e.g. to populate a second dropdown).

public void ajaxListener(AjaxBehaviorEvent event) {
    selectItemsOfSecondDropdown = populateItBasedOn(selectedValueOfFirstDropdown);
}

If you're actually also interested in the old value while executing a business action, then fall back to valueChangeListener, but queue it to the INVOKE_APPLICATION phase.

public void changeListener(ValueChangeEvent event) {
    if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
        event.setPhaseId(PhaseId.INVOKE_APPLICATION);
        event.queue();
        return;
    }

    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();
    System.out.println(newValue.equals(value)); // true
    // ...
}

@Lucas 2013-01-04 23:06:23

@BalusC, is there a reason not to obtain the old value from the backing object in the setter before setting it to the new value that is passed in? Something like this: logger.trace( "setting changeTypes from {} to {}", this.changeTypes, changeTypes );. It seems like you could use the old and new values obtained in that fashion to do business logic directly in the setter as well as simple logging, but I don't know if this would cause side effects...

@hbobenicio 2015-01-12 14:48:15

Perfect and complete answer! Thanks for sharing your knowledge!

@Paullo 2015-07-07 20:22:17

@BalusC is it possible to also get the changed value through AjaxBehaviorEvent? I have <h:selectManyListbox that is chained to other components and will like to use the changed (Added/Removed) to perform some action

@BalusC 2015-07-07 20:29:29

@Paullo 2015-07-07 20:58:59

Thanks @BalusC But i dont think ValueChangeListener will fit in my case as I have some execute and render values as shown <f:ajax event="valueChange" render="tests" execute="@this" listener="#{testController.processTimeTable}" />

@samouray 2016-01-21 15:09:59

I indeed found this answer helpfull regarding my question that I posted today stackoverflow.com/questions/34926653/…, I changed System.out.println("dateValueChanged : "+ date.toString()); with System.out.println("dateValueChanged : "+ event.getNewValue());

@The Bitman 2017-03-29 11:29:50

@Paullo Store the value progammatically in an instance attribute of the bean before change. At the next change you will have the value.

@a.u.r 2012-08-09 08:20:14

for the first fragment (ajax listener attribute):

The "listener" attribute of an ajax tag is a method that is called on the server side every time the ajax function happens on the client side. For instance, you could use this attribute to specify a server side function to call every time the user pressed a key

but the second fragment (valueChangeListener) :

The ValueChangeListener will only be called when the form is submitted, not when the value of the input is changed

*you might like to view this handy answer

Related Questions

Sponsored Content

1 Answered Questions

f:ajax listener not working with h:selectOneRadio

3 Answered Questions

[SOLVED] Mixing Ajax and full requests in JSF 2.0

3 Answered Questions

[SOLVED] JSF f:ajax listener not called

  • 2012-03-24 00:56:16
  • twmb
  • 14907 View
  • 0 Score
  • 3 Answer
  • Tags:   ajax jsf listener

1 Answered Questions

JSF 2.0: <f:ajax>'s listener is not triggered

0 Answered Questions

<f:ajax listener> not executed

  • 2014-01-21 12:32:38
  • Maximilian Schmidt
  • 1204 View
  • 0 Score
  • 0 Answer
  • Tags:   ajax jsf listener

1 Answered Questions

1 Answered Questions

0 Answered Questions

3 Answered Questions

[SOLVED] Valuechangelistener Doubt in JSF

  • 2011-01-26 05:01:42
  • Krishna
  • 19359 View
  • 7 Score
  • 3 Answer
  • Tags:   jsf

Sponsored Content