简体   繁体   English

为什么在ui:composition中未呈现ui:fragement

[英]Why is ui:fragement not rendered inside ui:composition

I'm a beginner with JSF and Java, please be nice! 我是JSF和Java的初学者,请保持友好!

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: 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: 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? 我是否真的缺少明显的东西?

Anything outside <ui:define> and <ui:composition> is ignored during building the view and don't end up in JSF component tree. 在构建视图期间, <ui:define><ui:composition>都将被忽略 ,并且最终不会出现在JSF组件树中。

You need to put <ui:fragment> inside <ui:define> . 您需要将<ui:fragment>放在<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: 一种替代方法是使用JSTL <c:if> ,因为它已在视图构建期间运行:

<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: 也可以看看:

<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. 尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM