0

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?

yisa
  • 51
  • 1
  • 1
  • 5

1 Answers1

1

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.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Once I change to , I get the correct id on Page1, however, when I run Page2 directly, the id is not displayed.. Maybe I should ask a different question. What I really want to do is making the Page1 to the main page, and including other auxiliary page Page2, Page3,etc when needed into Page1 as a tab. User can either choose which auxiliary page to view within Page1 or run other pages directly (different entry points). Is include the right approach to implement this? thanks for any pointers! – yisa Jul 22 '13 at 20:49
  • 1
    Included pages are not supposed to be accessed independently. Parameters are used to pass information from including page to the included page. These pages serve like fragments of the bigger picture. Be sure to reread the link in my answer to gain fuller understanding about compositioning in JSF. – skuntsel Jul 22 '13 at 20:57