JSTL c:如果在JSF中没有工作h:dataTable [英] JSTL c:if doesn't work inside a JSF h:dataTable

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

问题描述

我试图使用< c:if> 有条件地放置一个< h:outputLink> $ {code>< 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.

你想在这里使用呈现的属性。 >

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>



另请参见:




  • JSF2 Facelets中的JSTL有意义?

  • 有条件地显示JSF组件

  • See also:

    • JSTL in JSF2 Facelets... makes sense?
    • Conditionally displaying JSF components
    • 这篇关于JSTL c:如果在JSF中没有工作h:dataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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