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>
	    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-format="fluid"
     data-ad-layout="in-article"
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="6894224149"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script><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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Lucas Do AmaralAshish BansalJSF 2.0 Tutorial « java configHimalay Recent comment authors
newest oldest most voted
Lucas Do Amaral
Guest
Lucas Do Amaral

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
Guest
Ashish Bansal

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… Read more »

trackback
JSF 2.0 Tutorial « java config

[…] How to pass parameters to JSF 2.0 template file? JSF 2 <ui:param> example, pass parameter to an include file or template file. […]

Himalay
Guest
Himalay

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