将JSF标记与JSTL标记混合会产生奇怪的结果 [英] Mixing JSF tags with JSTL tags gives strange results

查看:115
本文介绍了将JSF标记与JSTL标记混合会产生奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

 <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。

As you see, i render that outputPanel only when columnRendered is true.

嗯,有时会出现这种情况(仅用于测试以批准它应该做什么):

Well, there are situations when this (used only for tests to approve what it should do):

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

为真所以它应该输入c:if并将columnRendered切换为false。但它没有,所以columnRendered永远是真的......

is true so it should enter in c:if and switch columnRendered to false. But it doesn't, so columnRendered is true forever...

你知道为什么吗?

推荐答案

JSF和JSTL没有按照您对编码的期望同步运行。 JSTL在视图的构建期间运行(当要填充JSF组件树时),并且JSF在视图组件树的渲染时间期间运行(当要生成HTML输出时)。您可以按如下方式对其进行可视化:JSTL首先从上到下运行,然后将结果移交给JSF,JSF又从上到下再次运行。

JSF and JSTL doesn't run in sync as you'd expect from the coding. JSTL runs during build time of the view (when the JSF component tree is to be populated) and JSF runs during render time of the view component tree (when HTML output is to be generated). You can visualize it as follows: JSTL runs first from top to bottom and then hands over the result to JSF which in turn runs from top to bottom again.

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

In your particular case, the object instance is never present in JSTL.

而不是 c:forEach ,你应该使用 ui:repeat 而不是 c:如果您应该使用JSF组件的呈现的属性。我想重写代码,但 hideTypes 的使用是一团糟。而是将其转换为模型中的 List< String> ,并且使用纯JSF会更容易。这是一个启动示例,假设 hideTypes 列表< String>

Instead of c:forEach, you should use ui:repeat and instead of c:if you should use JSF component's rendered attribute. I'd like to give a rewrite of the code, but the usage of hideTypes is a mess. Rather convert it to a List<String> in the model and it'll be much easier to do with pure JSF. Here's a kickoff example assuming that hideTypes is a List<String>:

<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>

这篇关于将JSF标记与JSTL标记混合会产生奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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