Phase listener to listen if there is no postback

Basically, I need to call the method after the RENDER_RESPONSE stage. I've tried this:

<f:phaseListener type="com.examples.MyPhaseListener"/>

      

The above listener listens all the time even for ajax calls. I tried

rendered="#{!facesContext.postback}" 

      

but it is not applicable here I guess. So I tried this as mentioned in this post Is it possible to disable f: event type = "preRenderView" postback listener? :

public void beforePhase(PhaseEvent pe) {
 if (!FacesContext.getCurrentInstance().isPostback()) {
          //do nothing
      }
}

public void afterPhase(PhaseEvent pe) {
      if (!FacesContext.getCurrentInstance().isPostback()) {
           if (pe.getPhaseId() == PhaseId.RENDER_RESPONSE) {
               //call a method
            }
      }
}

      

It works, but is there any other way to disable the listener after the original answer? I also tried preRenderComponent, but it gets called before the RENDER_RESPONSE phase and it looks like it's not rendering until the method is off the stack (it's mostly not asynchronous). So I feel like SystemEvents are not as big as preRenderView and preRenderComponent compared to calling them in PostConstruct.

+3


source to share


1 answer


There is really no other way to achieve the functional requirement. A Filter

in which you do the job after the call chain.doFilter(request, response)

should also work, but that doesn't give you access to the entity context (although a lot of JSF special data is also available with the standard Servlet API).

Regarding your phase listener, if you need to make it listen for only the render response, add

public PhaseId getPhaseId() {
    return PhaseId.RENDER_RESPONSE;
}

      

This way you don't need to check the current phase in the listener methods.



public void afterPhase(PhaseEvent event) {
    if (!FacesContext.getCurrentInstance().isPostback()) {
        // ...
   }
}

      

In the upcoming JSF 2.2, by the way, there is a new tag that understands isPostback()

, <f:viewAction>

:

<f:viewAction action="#{bean.action}" onPostback="false" />

      

However, it is triggered when only the action phase is invoked.

+3


source







All Articles