JSF 2 UI Param Example

When you invoke a template, you can supply arguments in two way: with ui:define and with the ui:param tags. As you have already seen; ui:define is used to provide markup that’s inserted into the template. In contrast, ui:param sets an Expression Language (EL) variable for use in a template. In the corresponding template, you can access the parameter with an EL expression. This is not the only use of ui:param, it also be used with the ui:include.

Also Read:

1. The Template

Decorator.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">
	<h:head>
		<title>JavaBeat UI Param Example</title>
	</h:head>
	<h:form>
		<h1><h:outputText value="JavaBeat JSF 2.2 Examples" /></h1>
		<h2><h:outputText value="JSF2 UI Param Example" /></h2>
		<h:panelGrid columns="2" border="1">
			<f:facet name="caption">
				<ui:insert name="caption"/>#{currentDate}
			</f:facet>
			<ui:insert name="item"/>
			<ui:insert name="amount"/>
		</h:panelGrid>
	</h:form>
</html>

2. The Included Page

include.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">
	<h1>This Page Is Included Via Include Facelets Tag</h1>
	<h2>This Page Is Included @ #{currentDate}</h2>
</html>

3. 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"
	xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
	<title><h:outputText value="JavaBeat UI Param Example"/></title>
</h:head>
<h:body>
	<ui:decorate template="/template/Decorator.xhtml">
		<ui:define name="caption">
			<h:outputText value="Expenses For Date : "/>
			<ui:param name="currentDate" value="#{indexBean.date}"></ui:param>
		</ui:define>
		<ui:define name="item">
			<h:panelGrid columns="1" border="1">
				<h:outputText value="Milk"/>
				<h:outputText value="Bread"/>
			</h:panelGrid>
		</ui:define>
		<ui:define name="amount">
			<h:panelGrid columns="1" border="1">
				<h:outputText value="10.5 USD"/>
				<h:outputText value="5.5 USD"/>
			</h:panelGrid>
		</ui:define>
	</ui:decorate>
	<ui:include src="/include.xhtml">
		<ui:param name="currentDate" value="#{indexBean.date}"></ui:param>
	</ui:include>
</h:body>
</html>

4. Managed Bean

indexBean.java

package net.javabeat.jsf;

import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class IndexBean {
	private Date date = new Date();

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
}

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>client</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 UI Param Demo

The below snapshot shows you the using of ui:param in conjunction with ui:include & template.

JSF 2 UI Param Example

Comments

comments

About Amr Mohammed

Speak Your Mind

*