JSTL c:if 在 JSF h:dataTable 中不起作用 [英] JSTL c:if doesn't work inside a JSF h:dataTable

查看:32
本文介绍了JSTL c:if 在 JSF h:dataTable 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 有条件地将 放入 状态结束时.

I'm trying to use <c:if> to conditionally put a <h:outputLink> inside a <h:dataTable> when the state is finished.

<h:dataTable value="#{bean.items}" var="item" width="80%">
    <h:column>
        <f:facet name="header">
            <h:outputText value="State" />
        </f:facet>

        <c:if test="#{item.state != 'Finish'}">
            <h:outputText value="Missing value" />
        </c:if>
        <c:if test="#{item.state == 'Finish'}">
            <h:outputLink value="myLink">
                <h:outputText value="Value = #{item.state}" />
            </h:outputLink>
        </c:if>
    </h:column>
</h:dataTable>

但这不起作用,为什么会这样,我该如何解决?

But this does not work, why is that and how can I fix it?

推荐答案

JSTL 标记在视图构建期间进行评估,而不是在视图呈现期间进行评估.您可以将其可视化如下:每当第一次创建视图树时,所有 JSTL 标记都会被执行,结果是一个只有 JSF 组件的视图.每当渲染视图树时,所有 JSF 组件都会执行,结果是 HTML.所以:JSF+JSTL 不会像您期望的编码那样同步运行.JSTL 首先从上到下运行,将结果交给 JSF,然后轮到 JSF 再次从上到下运行.这可能会导致 JSF 迭代 UIData 等组件出现意外结果,因为行数据(在您的特定情况下为 #{item} 对象)在 JSTL 运行时可用.

JSTL tags are evaluated during building of the view, not during rendering of the view. You can visualize it as follows: Whenever a view tree get created for the first time, all JSTL tags are executed and the result is a view with only JSF components. Whenever a view tree get rendered, all JSF components get executed and the result is HTML. So: JSF+JSTL doesn't run in sync as you'd expect from the coding. JSTL runs from top to bottom first, hands the result to JSF and then it's JSF's turn to run from top to bottom again. This may lead to unexpected results in JSF iterating components like UIData because the row data (in your particular case the #{item} object) is not available while JSTL runs.

简而言之:使用 JSTL 来控制 JSF 组件树构建的流程.使用 JSF 控制 HTML 输出生成的流程.

In a nutshell: Use JSTL to control flow of JSF component tree building. Use JSF to control flow of HTML output generation.

您想在此处使用 rendered 属性.

You want to use the rendered attribute here.

<h:outputText value="Missing value" rendered="#{item.state ne 'Finish'}" />
<h:outputLink value="myLink" rendered="#{item.state eq 'Finish'}">
    <h:outputText value="Value = #{item.state}" />
</h:outputLink>

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆