Search code examples
jsffacelets

Reuse some .xhtml pages on a JSF primefaces application


I developing a simple application using JSF and PrimeFaces and here's a problem that I'm facing:

These are managed beans that have a Person property:

  • ClientBean
  • EmployeeBean

I have the person.xhtml that shows the data from a person. I include the person.xhtml on a client.xhtml and employee.xhtml. I need to create two person.xhtml because I'm using different beans. What I want to do is something like that:

<c:set var="person" value="clientBean.person" /> 
<ui:include src="person.xhtml"/>

<c:set var="person" value="employeeBean.person" /> 
<ui:include src="person.xhtml"/>

And in my person.xhtml I can use #{person.name} , #{person.dateOfBirth}. I searched and use <c:set/> in JSF is wrong.

Anyone can help?


Solution

  • Pass it as <ui:param>.

    <ui:include src="person.xhtml">
        <ui:param name="person" value="#{clientBean.person}" /> 
    </ui:include>
    <ui:include src="person.xhtml">
        <ui:param name="person" value="#{employeeBean.person}" /> 
    </ui:include>
    

    Register person.xhtml if necessary as a tag file to make it look better, see also When to use <ui:include>, tag files, composite components and/or custom components?

    <my:personForm value="#{clientBean.person}" /> 
    <my:personForm value="#{employeeBean.person}" /> 
    

    Beware of duplicate component ID errors. See also Avoiding duplicate ids when reusing facelets compositions in the same naming container.