Search code examples
jsfjsf-2referenceuiinclude

Every include should refer to other instance


I include a part of the XHTML page to my Web Application. This part can be included multiple times. And that's the problem! Because every include refers to the same java object. That means that every element has the same value. But I want for each include a new Java object. So whats the best way to solve this problem?

Main page with includes:

<ui:composition template="./templates/template.xhtml">
<ui:define name="mainContent">

    <ui:include src="include/includeAbleEditor.xhtml">
        <ui:param name="includeParam" value="MyClass" />
    </ui:include>

    <ui:include src="include/includeAbleEditor.xhtml">
        <ui:param name="includeParam" value="YourClass" />
    </ui:include>

</ui:define>

includAbleEditor.xhtml

<h:commandButton value="#{editorVisibility.evb.value}"
                 action="#{editorVisibility.evb.toggle}" />

<h:inputTextarea rendered="#{editorVisibility.evb.enabled}" />

This <h:inputTextarea> is an example for my problem. Every included inputTextarea has a toggle button. By clicking the button the textarea should be shown or hidden. But because of the same reference of the boolean value all <h:inputTextarea> will always have the same rendered value.

Do you have any suggestions?

Thanks a lot!


Solution

  • You'll have to hold as many instances of editorVisibility.evb as you have editors. You could for example create a List<TypeOfEvb> evbList in your EditorVisibility bean, and pass only one element to the <ui:include> as a <ui:param>:

    Main page

    <ui:include src="include/includeAbleEditor.xhtml">
        <ui:param name="includeParam" value="MyClass" />
        <ui:param name="evb" value="#{editorVisibility.evbList[0]}" />
    </ui:include>
    

    includAbleEditor.xhtml

    <h:commandButton value="#{evb.value}"
                     action="#{evb.toggle}" />
    <h:inputTextarea rendered="#{evb.enabled}" />
    

    You could also create a composite component.

    See also:

    http://www.mkyong.com/jsf2/composite-components-in-jsf-2-0/