My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

sâmbătă, 10 octombrie 2015

Just tested JSF 2.3 PostRenderViewEvent under Payara 4.1

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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors