How to pass parameters to JSF 2.0 template file?

In JSF 2.0 web application, you can use “ui:param” facelets tag to pass parameters to an include file or a template file.

1. Parameter in “ui:include”

Example to pass a “tagLine” parameter to an include file.


<ui:insert name="header" >
   <ui:include src="/template/common/commonHeader.xhtml">

	<ui:param name="tagLine" value="JSF a day, bug away" />

   </ui:include>
</ui:insert>

In the commonHeader.xhtml file, you can access the “tagLine” parameter with a normal EL expression.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <body>
        <ui:composition>
	    <h2>Tag Line : #{tagLine}</h2>
	</ui:composition>
    </body>
</html>

2. Parameter in “ui:composition”

Example to pass a “curFileName” parameter to a template file.


<ui:composition template="template/common/commonLayout.xhtml">

	<ui:param name="curFileName" value="default.xhtml" />
			
</ui:composition>

In the commonLayout.xhtml file, you can access the “curFileName” parameter with a normal EL expression.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:body>

	<div id="content">
	        Current File : #{curFileName}
	</div>
	
    </h:body>
</html>

Download Source Code

Reference

  1. JSF <ui:param /> JavaDoc

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Lucas Do Amaral
10 years ago

Thank you for all the great classes you teach all of us!

When creating custom tags, how can I force that the params are informed (like you can do in a jsp custom tag)?

Ashish Bansal
13 years ago

well i think this example is not so clear

we can also try for this example

MyCommonHeader.xhtml

Here abc is the name of param, suppose we click on login and we assumed login is successful and outcome is UserPage.xhtml

This is yours personal Page …

so here we are sending param abc, here ub is the name of managedbean and username is the property of managed bean

………..

but in case if login gets fail.
then it returns back to Homepage.xhtml

in that case our homepage.xhtml should be like

Home Window

This Is My Home Page

here we are sending some msg when login gets fail , this msg may comes from managed bean as well.

Himalay
14 years ago

This is the simplest explanation of UI:Param I found online. Thanks!