JSF SystemEventListener – UIComponent Events Example

You’ve already examined the events that could be fired from both of Application and UIViewRoot. But those aren’t only the events that might be handled. The UIComponent - UIComponent is the base class for all user interface components in JavaServer Faces. The set of UIComponent instances associated with a particular request and response are organized into a component tree under a UIViewRoot that represents the entire content of the request or response - is also have a set of predefined system events could be listened.

The following events are fired by the UIComponent:

  • PostAddToViewEvent: After a component has been added to the view root.
  • PreRemoveFromViewEvent: Before a component is about to be removed.
  • PostRestoreStateEvent: After the state of a component has been restored.
  • PreValidateEvent: Before a component is validated.
  • PostValidateEvent: After a component is validated.
  • PreRenderComponentEvent: Before the component is about to be rendered.

At this tutorial, you are going to look at all mentioned events.

1. Managed Bean

IndexBean.java

package net.javabeat.jsf;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.PostRestoreStateEvent;
import javax.faces.event.PostValidateEvent;
import javax.faces.event.PreRemoveFromViewEvent;
import javax.faces.event.PreRenderComponentEvent;
import javax.faces.event.PreValidateEvent;

@ManagedBean
@ViewScoped
public class IndexBean {
	private UIComponentListener listener = new UIComponentListener();
	private UIInput messageInput;

	public IndexBean(){
		// For PreRemove
		FacesContext.getCurrentInstance().getApplication().
			subscribeToEvent(PreRemoveFromViewEvent.class, listener);
		// For PostRestore
		FacesContext.getCurrentInstance().getApplication().
			subscribeToEvent(PostRestoreStateEvent.class, listener);
	}

	private String message = "JavaBeat Message";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public UIComponentListener getListener() {
		return listener;
	}

	public void setListener(UIComponentListener listener) {
		this.listener = listener;
	}

	public UIInput getMessageInput() {
		return messageInput;
	}

	public void setMessageInput(UIInput messageInput) {
		this.messageInput = messageInput;
	}

	public void postAddToViewHandler(PostAddToViewEvent e){
		System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
	}

	public void preRenderComponent(PreRenderComponentEvent e){
		System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
	}

	public void preValidate(PreValidateEvent e){
		System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
	}

	public void postValidate(PostValidateEvent e){
		System.out.println("Event :: "+e.toString()+" :: Being Processed ::");
	}

	public String delete(){
		List<UIComponent> components =
				FacesContext.getCurrentInstance().getViewRoot().getChildren();
		for(UIComponent component : components){
			if(component.getId().equals(messageInput.getId())){
				FacesContext.getCurrentInstance().getViewRoot().getChildren().remove(component);
			}
		}
		return "";
	}
}

2. The View

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 prependId="false">
			<h1>JavaBeat JSF 2.2 Examples</h1>
			<h2>JSF2 System Events - UIComponent Events Example</h2>
			<br/>

			<h:panelGrid columns="3">
				<h:outputText value="Enter Your Message:">
					<f:event listener="#{indexBean.preRenderComponent}" type="preRenderComponent"></f:event>
				</h:outputText>
				<h:inputText value="#{indexBean.message}"
							binding="#{indexBean.messageInput}">
					<f:event listener="#{indexBean.postAddToViewHandler}" type="postAddToView"></f:event>
					<f:event listener="#{indexBean.preValidate}" type="preValidate"></f:event>
					<f:event listener="#{indexBean.postValidate}" type="postValidate"></f:event>
				</h:inputText>
				<h:commandButton value="Fire Remove From View Event" action="#{indexBean.delete}"></h:commandButton>
			</h:panelGrid>
		</h:form>
	</f:view>
</html>

3. SystemEventListener

UIComponentListener

package net.javabeat.jsf;

import javax.faces.component.UIComponent;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class UIComponentListener implements SystemEventListener {

	@Override
	public void processEvent(SystemEvent event) throws AbortProcessingException {
		System.out.println("Event :: "+event.toString()+" :: Being Handled");
	}

	@Override
	public boolean isListenerForSource(Object source) {
		if(source instanceof UIComponent){
			return true;
		}
		return false;
	}
}

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 SystemEventListener - UIComponent Events Demo

The below snapshot shows you the events propagated by the different type of UIComponent and the different ways that used for handle the events.

JSF SystemEventListener - UIComponent Events Demo

Comments

comments

About Amr Mohammed

Speak Your Mind

*