Tuesday, March 22, 2011

Passing parameter to included page using jsp:include in JSF 1.2

If you want to pass a parameter to an included page in JSF 1.2, it is not possible to do it like this:

<jsp:include page="header.jsp">
      <jsp:param value="main" name="pageTitle"/>
</jsp:include>

No way you can retrieve the parameter in header.jsp
This is because the JSF cycle already has built up it's view. Parameters you set while rendering, are not yet in the view that jsf created for this request. You could use the c:set tag and retrieve it using a scriptlet, but we want to do it clean and use no scriptlets in jsf, right.

The trick to do it is by binding a UIInput component in the first page to a certain value, and reading that value in the included file.

Say we have a request scoped bean (called pc_Header) HeaderBean.java with variable of type UIInput and name pageTitle. Then you can set the pagetitle in your main page like this:

<h:inputHidden binding="#{pc_Header.pagetitle}" value="My Title"></h:inputHidden>

This value can be used in the included header like this:

<h:outputText value=#{pc_Header.pagetitle.value}" />

No comments:

Post a Comment