Wednesday 7 November 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. :-)

4 comments :

Unknown said...

I tried this arrangement (JSF 1.1, RichFaces 3.1.3, WebLogic 9.2.2), but I got a very unhelpful exception:

javax.faces.FacesException: Assertion Failed
at com.sun.faces.util.Util.doAssert(Util.java:1302)
at com.sun.faces.taglib.jsf_core.ViewTag.getComponentType(ViewTag.java:241)
at javax.faces.webapp.UIComponentTag.createComponent(UIComponentTag.java:1013)
at javax.faces.webapp.UIComponentTag.createChild(UIComponentTag.java:1036)
at javax.faces.webapp.UIComponentTag.findComponent(UIComponentTag.java:749)
at javax.faces.webapp.UIComponentTag.doStartTag(UIComponentTag.java:429)
at com.sun.faces.taglib.jsf_core.ViewTag.doStartTag(ViewTag.java:105)

Unknown said...

Just to add a little more info: From what I can tell, the "getComponentType()" method just does this:

Util.doAssert(false);
throw new IllegalStateException();

So I guess there's an assumption that a subclass is supposed to be providing real functionality. I'm not sure why this would be happening.

Unknown said...

Oh, ok, never mind. It appears the included page has to use "f:subview", not "f:view".

However, I also see that variable references in the included page no longer work. I get errors like:

Error testing property 'propertyID' in bean of type null

(the references were working before moving them to the included page.)

shane said...

Thank you very much for the info...