JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

JSF 2 SystemEventListener – UIViewRoot Events Example

April 8, 2014 by Amr Mohammed Leave a Comment

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

[code lang=”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";
}
}
[/code]

2. UIViewRoot Event Listener

UIViewRootListener.java

[code lang=”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;
}
}
[/code]

3. The Views

index.xhtml

[code lang=”xml”]
<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>
[/code]

anonymousView.xhtml

[code lang=”xml”]
<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>
[/code]

4. Faces Configuration File

faces-config.xml

[code lang=”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>
[/code]

5. The Deployment Descriptor

web.xml

[code lang=”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>
[/code]

6. JSF 2 UIViewRoot Event Demo

The below snapshot shows you the normal behavior for listening upon UIViewRoot component events.

JSF 2 UIViewRoot Events Listening Example

[wpdm_file id=53]

Filed Under: JSF Tagged With: JSF 2

About Amr Mohammed

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved