Search code examples
jsffaceletsmanaged-beanuiinclude

ManagedBean inherits another MB. How to reuse the main class JSF as a component?


My problem is a little bit complex, but I'll give an example.

I already have a user.xhtml page (and a MB that uses it) where I register an user

<p:outputLabel value="Name:" /> <h:outputText value="#{userMB.bean.name}" />
<p:outputLabel value="e-mail:" /> <h:outputText value="#{userMB.bean.email}" />
etc...

Now I create an UserAdmMB that inherits UserMB, with another fields. I want to reuse the XHTML that exists, like using <ui:include>:

<ui:include src="user.xhtml" />
<p:outputLabel value="Address:" />
<h:outputText value="#{userAdmMB.anotherbean.address}" />

But the first XHTML already has userMB in it (to be used in another access point). How is the better way to do that?


Solution

  • Refactor the bean as <ui:param> of <ui:include>.

    The new include:

    <p:outputLabel value="Name:" /> <h:outputText value="#{user.name}" />
    <p:outputLabel value="Email:" /> <h:outputText value="#{user.email}" />
    ...
    

    The clients:

    <ui:include src="user.xhtml">
        <ui:param name="user" value="#{someBean.user}" />
    </ui:include>
    
    <ui:include src="user.xhtml">
        <ui:param name="user" value="#{someAdminBean.user}" />
    </ui:include>