0
votes

I'm new to JSF and trying to understand how include and param work, but have been stuck. Any help is much appreciated.

I have 2 simple pages (for testing purpose), Page1.xhtml and Page2.xhtml. I want to include Page2 into Page1 with one parameter using and in Page1. When I call Page2 directly, I can see the parameter being passed properly, but when I call Page1, Page2 is being included without the parameter. Below is the code.

Page1:

<h:body>
<h:form id="test">
<b>Page 1</b><br/>
<ui:include src="Page2.xhtml">
    <ui:param name="id" value="123" />
</ui:include>
<b>End of Page 1</b>
</h:form>

Page2:

<h:head> 
<f:view contentType="text/html"></f:view>
</h:head>

<h:body>
<h:form>
    <h:outputLabel for="ID" value="ID on Page2: "/>
    <h:outputText id="ID" value="#{pageTestBean.id}"/>
</h:form>
</h:body>
</html>

PageTestBean: @ManagedBean @SessionScoped public class PageTestBean {

private Long id=new Long(11111);

public void init() {
    //doesn't do anything yet;
}

// Getters and Setters

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

}

I'm expecting to see "123" as the output Id on Page1, not "11111" which is the default value when no parameter is passed in. However, I always see 11111. Is my expectation wrong?

1

1 Answers

1
votes

First of all, your include is handled inappropriately: the incuded page should be composed solely of <ui:composition> like the following one:

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    ...incuded page content...
</ui:composition>

An excellent point of reference is BalusC's answer to How to include another XHTML in XHTML using JSF 2.0 Facelets?.

Next, the included parameter is to be accessed simply via #{paramName} in the included page, like in:

<h:outputText value="#{paramName}" />

Parameter name is id in your case.

There are some other drawbacks of your code, like abusing session scope and nested HTML forms, but that's another question. The last but not the least is the thing that you have to understand how to deal with managed beans in views.