< c:if test>似乎总是在JSF2 Facelets中评估为true [英] <c:if test> seems to always evaluate true in JSF2 Facelets

查看:52
本文介绍了< c:if test>似乎总是在JSF2 Facelets中评估为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Facelets上使用JSF2.

I am using JSF2 on Facelets.

我在页面中定义了<ui:param>:

<ui:composition template="/WEB-INF/templates/ui.xhtml"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
>
  <ui:param name="title" value="OnAir WebDemo" />
  ...
</ui:composition>

我在ui.xhtml模板中的

:

in the ui.xhtml template I have:

<html
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:c="http://java.sun.com/jstl/core"
>
  <c:if test="#{not empty title}">
    <h1>#{title}</h1>
  </c:if>
</html>

但是,即使未指定<ui:param><c:if test>似乎总是对true求值.如何更改代码,以便在未指定<ui:param>的情况下<c:if test>实际评估false?

However, <c:if test> seems to always evaluate true, also if the <ui:param> is not specified. How can I change the code so that <c:if test> will actually evaluate false when the <ui:param> is not specified?

推荐答案

XML名称空间无效.应该是

The XML namespace is invalid. It should be

xmlns:c="http://java.sun.com/jsp/jstl/core"

是的,令人惊讶的是URI中的/jsp部分!不带/jsp的版本仅在Facelets 1.x(适用于JSF 1.x)中工作.如果您已经在Web浏览器中检查了生成的HTML输出,则还应该注意<c:if>在HTML输出中未解析.

Yes, astonishingly with the /jsp part in the URI! The one without /jsp works only in Facelets 1.x for JSF 1.x. If you have checked the generated HTML output in the webbrowser, you should have noticed as well that the <c:if> is left unparsed in the HTML output.

也就是说,您应该使用JSF组件而不是JSTL标记,除非技术上不可能(例如,当您实际上要控制视图的 building 而不是视图的 rendering 时)看法).您发现自己的h:panelGroup是一个不错的选择,但是ui:fragment是一个更好的选择,因为它的开销较小.

That said, you should prefer JSF components over JSTL tags, unless technically impossible (i.e. when you actually want to control the building of the view, not rendering of the view). The h:panelGroup as you found out yourself is a good candidate, however the ui:fragment is a nicer choice since it has less overhead.

<ui:fragment rendered="#{not empty title}">
    <h1>#{title}</h1>
</ui:fragment>

请注意,由于最初的JSF 2.0版本的<ui:fragment>标记定义文件中的JSF伙计有误,Netbeans会认为该标记不支持rendered属性,但这是不正确的.它当然支持它.它已在JSF 2.1标记定义中修复.

Note that due to a mistake of the JSF guys in the <ui:fragment> tag definition file of the initial JSF 2.0 version, Netbeans will jerk that the tag doesn't support the rendered attribute, but this is untrue. It certainly supports it. It has been fixed in JSF 2.1 tag definition.

  • JSTL in JSF2 Facelets... makes sense?
  • Alternative to ui:fragment in JSF
  • Conditional rendering of non-JSF components (plain vanilla HTML and template text)

这篇关于&lt; c:if test&gt;似乎总是在JSF2 Facelets中评估为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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