As mentioned previously in the introduction of the JSF2 System Events – Application Events, there are many kinds of System events that could be listening for. UIViewRoot component – is the UIComponent that represents the root of the UIComponent tree – also does considered one of the component that has fired a system event.
UIViewRoot has fired three main types of events:
- PreRenderComponentEvent: This event has fired before the view root is about to be rendered.
- PostConstructiveMapEvent: This event has fired after the root component has constructed the view scope map
- PreDestroyViewMapEvent: This event has fired when the view map is cleaned.
Let’s see how could such those events listened.
1. ViewScoped Managed Bean
IndexBean.java
package net.javabeat.jsf; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.context.FacesContext; import javax.faces.event.ComponentSystemEvent; import javax.faces.event.PostConstructViewMapEvent; import javax.faces.event.PreDestroyViewMapEvent; @ManagedBean @ViewScoped public class IndexBean { private UIViewRootListener listener = new UIViewRootListener(); public IndexBean(){ // Subscribe listener for PostConstructiveViewMapEvent FacesContext.getCurrentInstance().getApplication(). subscribeToEvent(PostConstructViewMapEvent.class, listener); // Subscribe listener for PreDestrotyViewMapEvent FacesContext.getCurrentInstance().getApplication(). subscribeToEvent(PreDestroyViewMapEvent.class, listener); } private String message = "JavaBeat Message"; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void preRenderView(ComponentSystemEvent e){ System.out.println("PreRenderView Event Being Processed"); } public String emptyViewScope(){ return "index"; } }
2. UIViewRoot Event Listener
UIViewRootListener.java
package net.javabeat.jsf; import javax.faces.component.UIViewRoot; import javax.faces.event.AbortProcessingException; import javax.faces.event.SystemEvent; import javax.faces.event.SystemEventListener; public class UIViewRootListener implements SystemEventListener{ public void processEvent(SystemEvent event) throws AbortProcessingException { System.out.println("System Event : "+event.toString() + " :: Being Processed"); } public boolean isListenerForSource(Object source) { if(source instanceof UIViewRoot){ return true; } return false; } }
3. The Views
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <f:view> <h:form> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 System Events - Application Source Example</h2> <br/> <h2>This is a JSF View</h2> <h:commandButton value="Go To Anonymous View" action="anonymousView"></h:commandButton> </h:form> </f:view> </html>
anonymousView.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <f:view> <f:event listener="#{indexBean.preRenderView}" type="preRenderView"></f:event> <h:form> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 System Events - Application Source Example</h2> <br/> <h3>View Map Constructed</h3> <br/> <h:outputText value="#{indexBean.message}"/> <br/> <h:commandButton value="Go To Index" action="index"></h:commandButton> </h:form> </f:view> </html>
4. Faces Configuration File
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <resource-bundle> <base-name>net.javabeat.jsf.application</base-name> <var>msg</var> </resource-bundle> </application> </faces-config>
5. The Deployment Descriptor
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" metadata-complete="true"> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2 </description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>server</param-value> </context-param> <context-param> <param-name>javax.faces.application.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>
6. JSF 2 UIViewRoot Event Demo
The below snapshot shows you the normal behavior for listening upon UIViewRoot component events.
[wpdm_file id=53]