Passing parameters in JSF Facelets Templating
In this tutorial we show you how to pass parameters in JSF using Facelets Templating. We will be passing parameters down the chain using the <ui:param>
element.
Main Layout Template
This is our main layout template. We insert the header template and pass a passedHeaderTitle into the template.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<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:head>
<title><ui:insert name="title">default title</ui:insert></title>
<h:outputStylesheet library="css" name="style.css"/>
</h:head>
<h:body>
<ui:insert name="header" >
<ui:include src="/templates/common/header.xhtml">
<ui:param name="passedHeaderTitle" value="#{headerTitle}" />
</ui:include>
</ui:insert>
</h:body>
</html>
Header Template
The header template will print two parameters on to the page. The first is passed from the main layout, and the other is passed down from the index.xhtml page.
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<header>
<h1>Header with passed param: #{passedHeaderTitle}</h1>
<h1>Header with param from index.xhtml: #{headerTitle}</h1>
</header>
</ui:composition>
Index.xhtml composition
We use the main-layout as our template and define a title, and pass a parameter down the chain named headerTitle
<ui:composition
template="/templates/layout/main-layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<!-- override title attribute -->
<ui:define name="title">index.html</ui:define>
<!-- pass parameter headerTitle -->
<ui:param name="headerTitle" value="custom header title" />
</ui:composition>
Demo
URL: http://localhost:8081/jsf-templating-pass-parameters/