1

I'm a beginner with JSF and Java, please be nice!

I'm trying to render a specific block if a user is an Administrator.

I have a template. This template render correctly a specific block if the user is logged.

admin.xhtml:

    <ui:composition template="../../WEB-INF/tpl/admin/template.xhtml">
        <ui:define name="sectionTitle">Admin</ui:define>

        <!-- Logged as Admin -->
        <ui:fragment rendered="#{user.admin}">
            <ui:define name="body">
                <h3>Welcome</h3>
                <p>Please choose an administration task!</p>
            </ui:define>
        </ui:fragment>

    </ui:composition>

template.xhtml:

<div class="header">
    <h2>
        <ui:insert name="sectionTitle"> Default Section Title</ui:insert>
    </h2>
    <ui:insert name="logBox">
        <ui:fragment rendered="#{not user.logged}">
            <ui:include src="login.xhtml"></ui:include>
        </ui:fragment>

        <ui:fragment rendered="#{user.logged}">
            <h:form>
                <h:commandButton value="Logout"
                    action="#{userService.logout}" />
            </h:form>
        </ui:fragment>
    </ui:insert>
</div>

    <div class="corebody">
        <ui:insert name="body"> Default Section Content</ui:insert>
    </div>

I can move the rendered block inside the template like so:

<ui:fragment rendered="#{user.admin}">
        <ui:insert name="body"> Default Section Content</ui:insert>
</ui:fragment>

but this don't seem ok to me regarding the responsability of each files ( this don't seem really generic, why should such a thing be in the template? ).

Am I missing something really obvious?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
achap
  • 33
  • 4
  • This [**link**](http://stackoverflow.com/questions/3713468/alternative-to-uifragment-in-jsf) may help you – Husam Dec 12 '13 at 15:16

2 Answers2

2

Anything outside <ui:define> and <ui:composition> is ignored during building the view and don't end up in JSF component tree.

You need to put <ui:fragment> inside <ui:define>.

<ui:composition template="../../WEB-INF/tpl/admin/template.xhtml">
    <ui:define name="sectionTitle">Admin</ui:define>

    <!-- Logged as Admin -->
    <ui:define name="body">
        <ui:fragment rendered="#{user.admin}">
            <h3>Welcome</h3>
            <p>Please choose an administration task!</p>
        </ui:fragment>
    </ui:define>

</ui:composition>

An alternative is to use JSTL <c:if> as that runs during view build time already:

<ui:composition template="../../WEB-INF/tpl/admin/template.xhtml">
    <ui:define name="sectionTitle">Admin</ui:define>

    <!-- Logged as Admin -->
    <c:if test="#{user.admin}">
        <ui:define name="body">
            <h3>Welcome</h3>
            <p>Please choose an administration task!</p>
        </ui:define>
    </c:if>

</ui:composition>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BasluC: I'm trying to follow your example with JSTL and it's not working :(. I'm working with JSF1.2, could it be that? I already tried with different namespaces. – Gabriel Feb 01 '17 at 15:33
  • I already asked a question that no one answered about a week ago: http://stackoverflow.com/questions/41810854/jsf-1-2-dynamic-uiparam – Gabriel Feb 01 '17 at 15:37
  • 1
    @Gabriel: use [jsf] tag to get attention from JSF users, not only a version tag. – BalusC Feb 01 '17 at 17:47
0
<ui:composition template="../../WEB-INF/tpl/admin/template.xhtml">
    <ui:define name="sectionTitle">

        <!-- Logged as Admin -->
        <ui:fragment rendered="#{user.admin}">
            <ui:define name="body">
                 <h3>Welcome</h3>
                 <p>Please choose an administration task!</p>
            </ui:define>
        </ui:fragment>
    </ui:define>
</ui:composition>

Try this.

Flying Dumpling
  • 1,294
  • 1
  • 11
  • 13