Wednesday, November 7, 2007

JSF Passing parameters to included jsp pages


The question was brought up in the JDeveloper forum. When I started creating my first JSF pages I wanted to be able to create a menu that I could edit and make make the changes propagate to all pages immediately. So What I wanted back then was amethod to include a jsp to all my pages but also be able to pass parameters from the including page to the one being included.



The answer came from the book Mastering JavaServer Faces by Bill Dudney, Jonathan Lehr, Bill Willis and LeRoy Mattingly.



The approach is to create a subview and then include the page using a jsp:include tag. Inside the include you must use a jsp:param tag where you define the name and value of the expected JSP parameter. To cut the long story short, I tested the solution with JDeveloper 10.1.3.3 and here is the code fragment to use.



<f:subview id="header">
<jsp:include page="/header.jsp" flush="true">
<jsp:param name="pageTitle" value="Welcome to my first JSF Application"/>
</jsp:include>
</f:subview>


The included page should be surrounded by an f:subview tag.
The external parameter can be accessed by an EL expression like "#{param.paramName}" and a simple example would be something like :



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=ISO-8859-7"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://xmlns.oracle.com/adf/faces" prefix="af"%>
<%@ taglib uri="http://xmlns.oracle.com/adf/faces/html" prefix="afh"%>
<f:subview id="header">
<afh:html>
<afh:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7"/>
</afh:head>
<afh:body>
<af:panelHeader text="#{param.pageTitle}"/>
</afh:body>
</afh:html>
</f:subview>

... and one last comment. I guess that this is the first time that the Copy as HTML command works correctly in JDeveloper on Linux. :-)

0 comments:

Post a Comment