11

I have a Facelet that might be used in different applications. I don't to copy it, but reuse it. I need to pass the backing bean that will manage the view as a parameter, as some logic may vary according to the application where it is used in.

I don't want to use a composite component, but just include the Facelet and specify which bean will manage the view. How can I achieve this?

Let me give an example:

<ui:composition template="/resources/common/templates/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
    <ui:define name="content">
        <!-- somehow establish the backing bean that will manage formView.xhtml --> 
        <!-- f:set  assign="ParameterBean" value="#{Bean}" / -->
        <ui:include src="formView.xhtml" />
    </ui:define>
</ui:composition>

formView.xhtml :

<ui:composition template="/resources/common/templates/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
    <ui:define name="content">
        <h:outputText value="#{ParameterBean.texto}" />
    </ui:define>
</ui:composition>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Deibys
  • 619
  • 3
  • 9
  • 18

2 Answers2

24

You can use <ui:param> for that. It needs to be nested in the <ui:include>.

<ui:include src="formView.xhtml">
    <ui:param name="ParameterBean" value="#{Bean}" />
</ui:include>

Unrelated to the concrete problem, standard Java Naming Conventions state that instance variable names must start with lower case. You should change your code in such way that respectively parameterBean and #{bean} will be used.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Because I would have found it helpful yesterday, when I was looking for this, here is a simple version of how to do this, without the extraneous template, defines and namespaces:

File1.xhtml (the root tag doesn't matter)

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

File2.xhtml

<ui:composition ... xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets" ... >
  <h:outputLabel value="#{person.name}" />
</ui:composition>


You can also nest further in the same manner.

File1.xhtml

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

File2.xhtml

<ui:composition ... xmlns:ui="http://java.sun.com/jsf/facelets" ... >
  <ui:include src="File3.xhtml">
    <ui:param name="name" value="#{person.name}" />
  </ui:include>
</ui:composition>

File3.xhtml

<ui:composition ... xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets" ... >
  <h:outputLabel value="#{name.length}" />
</ui:composition>
Chris
  • 3,400
  • 1
  • 27
  • 41
  • I fail to see how this is different fromthe answer below? – Kukeltje Aug 05 '15 at 18:02
  • I had a hard time understanding exactly how the answer was supposed to be used so I tried to make it more obvious for others. – Chris Aug 06 '15 at 14:30
  • Then you can 'edit' the question and remove these things and mention in the 'comment' why you did this. When others 'accept' the change, it will be visible. No need to create a second answer then. – Kukeltje Aug 06 '15 at 14:44