选择中的EL表达式

选择中的EL表达式

本文介绍了c:选择中的EL表达式,无法使其正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试类似这样的东西(JSF2):

I'm trying something (JSF2) like this:

<p>#{projectPageBean.availableMethods}</p>
<c:choose>
  <c:when test="${projectPageBean.availableMethods == true}">
    <p>Abc</p>
  </c:when>
  <c:otherwise>
    <p>Xyz</p>
  </c:otherwise>
</c:choose>

但这似乎不起作用,尽管顶部的EL表达式从false变为是的,下一段总是显示Xyz吗?

But this doesn't seem to work, although the EL expression in the top paragraph changes from false to true, the next paragraph always shows Xyz?

我还尝试将测试更改为:

I also tried to change the test to:

${projectPageBean.availableMethods}

但仍然是同样的问题!

But still the same problem!

推荐答案

首要的是:。

First and foremost: JSTL tags runs during view build time, not during view render time.

您的具体问题表明#{projectPageBean} 是在视图渲染期间设置的,例如,将其定义为< ui:repeat var> < h:dataTable var> < p:tabView var> 等,因此为 null 在视图构建期间。

Your concrete problem suggests that #{projectPageBean} is been set during view render time, such as would happen when definied as <ui:repeat var>, <h:dataTable var>, <p:tabView var>, etc. It's thus null during view build time.

在这种情况下,您不应使用视图构建时间标记来控制按传统方式呈现HTML。您应该改为使用视图渲染时间组件来有条件地渲染HTML。首选使用< ui:fragment>

In that case, you should not be using a view build time tag to conditionally render HTML. You should instead use a view render time component to conditionally render HTML. As first choice, use <ui:fragment>:

<p>#{projectPageBean.availableMethods}</p>
<ui:fragment rendered="#{projectPageBean.availableMethods}">
    <p>Abc</p>
</ui:fragment>
<ui:fragment rendered="#{not projectPageBean.availableMethods}">
    <p>Xyz</p>
</ui:fragment>

在Facelets中,无需在#{} $ {} 。与JSP相反,在Facelets中, $ {} 的行为与#{} 完全相同。为了避免潜在的混乱和维护麻烦,我建议始终坚持使用#{}

By the way, there's in Facelets no need to switch between #{} and ${}. In contrary to JSP, in Facelets the ${} behaves exactly the same as #{}. To avoid potential confusion and maintenance trouble, I recommend to stick to #{} all the time.



  • Conditional rendering of non-JSF components (plain vanilla HTML and template text)

这篇关于c:选择中的EL表达式,无法使其正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:09