As you
probably know, JSF 2.2 comes with a significant number of events, but none of
them is published right after the view is rendered. With other words, the PreRenderViewEvent
doesn't have a "friend" of type PostRenderViewEvent. Well,
starting with JSF 2.3 this is not true anymore, because PostRenderViewEvent is
available and comes to add consistency to JSF events suite.
Since Mojarra
2.3.0 Milestone 4 contains PostRenderViewEvent already, I was playing a
little bit with it under Payara 4.1. So, I have wrote two simple examples as follows:
·
Subscribe via <f:event> from page to listen PostRenderViewEvent.
In order to highlight the PostRenderViewEvent place, I've also
subscribed to PreRenderViewEvent
and added a custom PhaseListener for Render Response phase. Notice that the PostRenderViewEvent
is available before afterPhase() for Render Response.
<h:body>
<f:event type="preRenderView"
listener="#{renderBean.preRenderAction()}" />
<f:event type="postRenderView"
listener="#{renderBean.postRenderAction()}" />
</h:body>
The RenderBean
is just a dummy managed bean that logs some text in preRenderAction() and postRenderAction()
methods:
@Named
@RequestScoped
public class
RenderBean {
private static final Logger LOG =
Logger.getLogger(RenderBean.class.getName());
public void preRenderAction(){
LOG.info("RenderBean#preRenderAction()
...");
}
public void postRenderAction(){
LOG.info("RenderBean#postRenderAction()
...");
}
}
And the PhaseListener
is also logging some text:
public class
RenderPhaseListener implements PhaseListener {
private static final Logger LOG =
Logger.getLogger(RenderPhaseListener.class.getName());
@Override
public void afterPhase(PhaseEvent event) {
LOG.info("RenderPhaseListener#afterPhase() ...");
}
@Override
public void beforePhase(PhaseEvent event) {
LOG.info("RenderPhaseListener#beforePhase()
...");
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
}
Now, the
output will reveal the moment when PostRenderViewEvent is processed:
RenderPhaseListener#beforePhase()
...
RenderBean#preRenderAction()
...
RenderBean#postRenderAction() ...
RenderPhaseListener#afterPhase()
...
The complete
code is available here.
·
Subscribe programmatically to listen PostRenderViewEvent
from a custom component. Again I used some log text to reveal the moment when
the PostRenderViewEvent
is processed with respect to encodeBegin() and encodeEnd() methods:
@FacesComponent(value
= TomComponent.COMPONENT_TYPE, createTag = true)
public class
TomComponent extends UIComponentBase implements SystemEventListener {
private static final Logger LOG =
Logger.getLogger(TomComponent.class.getName());
public static final String COMPONENT_FAMILY =
"jsf.uicomponentwithsubscribetoevent";
public static final String COMPONENT_TYPE =
"jsf.uicomponentwithsubscribetoevent.TomComponent";
public TomComponent() {
FacesContext.getCurrentInstance().getViewRoot().
subscribeToViewEvent(PreRenderViewEvent.class, this);
FacesContext.getCurrentInstance().getViewRoot().
subscribeToViewEvent(PostRenderViewEvent.class, this);
}
@Override
public void processEvent(SystemEvent event)
throws AbortProcessingException {
LOG.log(Level.INFO, "EVENT EMITTED:
{0}", event);
}
@Override
public void encodeBegin(FacesContext context)
throws IOException {
LOG.log(Level.INFO,
"TomComponent#encodeBegin()");
}
@Override
public void encodeEnd(FacesContext context)
throws IOException {
LOG.log(Level.INFO,
"TomComponent#encodeEnd()");
}
@Override
public String getFamily() {
return COMPONENT_FAMILY;
}
@Override
public boolean isListenerForSource(Object
source) {
return true;
}
}
And, the output
will be:
EVENT
EMITTED:
javax.faces.event.PreRenderViewEvent[source=javax.faces.component.UIViewRoot]
TomComponent#encodeBegin()
TomComponent#encodeEnd()
EVENT EMITTED:
javax.faces.event.PostRenderViewEvent[source=javax.faces.component.UIViewRoot]
Obviously, PostRenderViewEvent
hits processEvent()
after encodeEnd(),
which means that the rendering process is over.
The complete
application is available here.
In real
applications, you can use PostRenderViewEvent for post rendering view
processing, state handling, cleaning view specific things, etc.
Niciun comentariu :
Trimiteți un comentariu