Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrimeFaces Ajax Listener not executed when process attribute is specified for a different component

When I specify process attribute of p:ajax tag, the listener is not executed. If I omit the process attribute, then the listener is called as expected.

Here is the view snippet:

<p:messages id="messages" />
<h:inputText id="inputToProcess" value="#{controller.inputToProcess}" />
<p:selectBooleanCheckbox id="testCheckbox" >
  <p:ajax event="change" process="inputToProcess"
    update="messages @this inputToUpdate"
    listener="#{controller.processChecked}" />
</p:selectBooleanCheckbox>
<h:inputText id="inputToUpdate" value="#{controller.inputToUpdate}" />

And Controller:

@javax.faces.bean.ManagedBean
@javax.faces.bean.ViewScoped
public class Controller implements Serializable {
  private String inputToProcess;
  private String inputToUpdate;
  //getters and setters

  public void processChecked(javax.faces.AjaxBehaviorEvent e) {
    // doing some stuff here
  }
}

I attached a phaseListener to a view with ANY_PHASE PhaseId, and here is what I observed.

When I specify process attribute , the value of the inputToProcess input is successfully set to the controller during the Update Model phase (no exception occurs). Then the Invoke Application and Render Response phases are executed, but no listener is called. One thing I noticed is that checkbox is not set in the end. But, there are no conversion or validation errors, because as I said the Update Model and Invoke Application phases are executed.

If I omit process attribute, here is what I see: the listener is normally called during the Invoke Application phase (since immediate is false by default), and then `Render Response is executed. Checkbox is successfully set.

Is there any explanation for this sort of behavior?

like image 649
jFrenetic Avatar asked Apr 23 '12 19:04

jFrenetic


1 Answers

It should work fine at first sight. At least, it works fine that way when using standard JSF components. I'd bet it to be a bug or "feature" of PrimeFaces that it doesn't process the action when the action component is not included in the process. Adding @this to process should solve it. Consider posting an issue report to PrimeFaces guys.

Further, I'd rather use event="valueChange" or event="click" instead of event="change" or just remove the event altogether, it defaults to the right value already (valueChange which will render onclick in checkbox and radio button components). The change event works differently in MSIE for checkboxes and radiobuttons. It's only triggered on 2nd click and forth. You don't want to be browser dependent.


As per your comment:

The problem with the standard JSF checkbox and ajax components, is that the listener is invoked during Process Validations phase, but I need to update the model first!

This is not true. Probably you was using valueChangeListener instead of <f:ajax listener> or confusing the one with the other. The <f:ajax listener> is always invoked during invoke action phase.

like image 142
BalusC Avatar answered Nov 12 '22 19:11

BalusC