有条件地渲染ui:define

时间:2012-12-03 02:10:41

标签: jsf facelets templating conditional-rendering

如何有条件地呈现<ui:define>

模板中的数据取决于所需的<f:viewParam>

但是如果提供了无效的视图参数,则不应呈现<ui:define>,因为应该使用模板的默认内容。

我尝试使用<c:if>,但它无效。

2 个答案:

答案 0 :(得分:12)

无法有条件地渲染/构建<ui:define>。你只能为它的内容做。模板中会忽略<ui:define>以外的任何内容。

最好是有条件地构建<ui:insert><ui:insert>在视图构建期间运行,因此无法通过<f:viewParam>设置的属性有条件地呈现它。您只能使用条件视图构建时标记(例如JSTL <ui:insert>)有条件地构建它(<c:if>标记本身),而后者又直接检查原始请求参数(因此不是<f:viewParam>属性)。

在主模板中,它看起来像这样,假设只有请求参数的唯一存在就足够了(如果你需要执行验证,你需要在EL表达式中或在托管中执行它其中财产通过@ManagedProperty("#{param.foo}")预先初始化的bean。

,例如,在主模板中:

<c:if test="#{not empty param.foo}">
    <ui:insert name="content" />
</c:if>
<c:if test="#{empty param.foo}">
    <p>Default content</p>
</c:if>

或者,更清晰但更详细

<c:choose>
    <c:when test="#{not empty param.foo}">
        <ui:insert name="content" />
    </c:when>
    <c:otherwise>
        <p>Default content</p>
    </c:otherwise>
</c:choose>

另见:

答案 1 :(得分:1)

改为使用<ui:fragment>

<ui:define name="description">
    <ui:fragment rendered="false">
        <meta name="description" content="do not render" />
    </ui:fragment>
</ui:define>

重复ui:define with rendered="false" attribute still rendering

相关问题