【问题标题】:Mixing JSF tags with JSTL tags gives strange results将 JSF 标记与 JSTL 标记混合会产生奇怪的结果
【发布时间】:2011-05-08 11:28:17
【问题描述】:

我有这段代码:

 <c:if test="#{utils.getCounterOfCharOccurence(hideTypes, ';') != 0}">
   <ui:repeat value="#{document.instanceList}" var="instance">
    <c:set var="columnRendered" value="true"></c:set>
    <c:forEach items="${hideTypes.split(';')}"
               var="hideType">
     <h:outputText value="#{hideType eq instance.documentInstanceType.mimeType}"/>
     <c:if test="#{hideType eq instance.documentInstanceType.mimeType}">
      <c:set var="columnRendered" value="false"></c:set>
      <h:outputText value="#{columnRendered}|"/>
     </c:if>
    </c:forEach>
    <a:outputPanel rendered="#{columnRendered == 'true'}">
     <up:mimeTypeIcon type="#{instance.documentInstanceType.mimeType}"
                      icon="#{instance.documentInstanceType.iconPath}"
                      key="#{instance.instanceKey}" referenced="false"/>
    </a:outputPanel>
   </ui:repeat>

  </c:if>

如您所见,我仅在 columnRendered 为 true 时才渲染该 outputPanel。

嗯,在某些情况下(仅用于测试以批准它应该做什么):

<h:outputText value="#{hideType eq instance.documentInstanceType.mimeType}"/>

为真,因此应输入 c:if 并将 columnRendered 切换为假。但事实并非如此,因此 columnRendered 永远为真......

你知道为什么吗?

【问题讨论】:

    标签: java jsf jstl


    【解决方案1】:

    JSF 和 JSTL 不会像您对编码所期望的那样同步运行。 JSTL 在视图的构建期间运行(当要填充 JSF 组件树时),而 JSF 在视图组件树的渲染期间运行(当要生成 HTML 输出时)。您可以将其可视化如下:JSTL 首先从上到下运行,然后将结果交给 JSF,JSF 又从上到下运行。

    在您的特定情况下,对象 instance 永远不会出现在 JSTL 中。

    您应该使用ui:repeat 而不是c:forEach,而不是c:if,您应该使用JSF 组件的rendered 属性。我想重写代码,但hideTypes 的用法一团糟。而是将其转换为模型中的List&lt;String&gt;,使用纯 JSF 会容易得多。这是一个假设 hideTypesList&lt;String&gt; 的启动示例:

    <h:panelGroup rendered="#{not empty hideTypes}">
        <ui:repeat value="#{document.instanceList}" var="instance">
            <a:outputPanel rendered="#{!hideTypes.contains(instance.documentInstanceType.mimeType)}">
                <up:mimeTypeIcon type="#{instance.documentInstanceType.mimeType}"
                    icon="#{instance.documentInstanceType.iconPath}"
                    key="#{instance.instanceKey}" referenced="false"/>
            </a:outputPanel>
         </ui:repeat>
    <h:panelGroup>
    

    【讨论】:

    • 嗨。感谢您的评论...问题是,当且仅当 document.instanceList 的 mime 类型中没有隐藏类型时,我想渲染该 outputPanel...所以检查每个 doc 实例 mime 类型集以查看是否在 hideTypes放。如果没有,请渲染它....我想我会在 java iso jsf 中完成它:)
    • gr8..但是 hideTypes 是 String[]。 jsf中是否有任何方法,例如: Arrays.asList(...).contains(...) ?
    • 一个糟糕的模型设计不应该在视图中解决,而应该在模型本身中解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多