尽管 ui:fragment rendered="false",值表达式仍然被评估

Value expressions still evaluated despite ui:fragment rendered="false"

我有豆子:

class Property{
 private String type;
 private Date value;
 //getters and setters
}

页面上还有代码块:

<ui:fragment rendered="#{property.type eq 'checkbox'}">
    <ui:include src="checkbox.xhtml">
        <ui:param name="property" value="#{property}"/>
    </ui:include>
</ui:fragment>

checkbox.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
    <body>
        <ui:composition>
            <h:selectBooleanCheckbox value="#{property.value}"/>
        </ui:composition>
    </body>
</html>

条件#{property.type eq 'checkbox'} = false

但我得到下一个例外:

javax.servlet.ServletException: checkBox.xhtml value="#{property.value}": Cannot convert 01.11.02 0:00 of type class java.util.Date to class java.lang.Boolean

我预计如果 ui:include 中的属性 rendered=false,那么这个块将不会被处理。

<ui:fragment rendered> 阻止它呈现 HTML 输出,但它不会阻止它在 JSF 组件树中结束并有资格进行状态保存。

改用<c:if test>。它在视图构建期间运行,而不是在视图渲染期间运行,因此整个组件根本不会出现在 JSF 组件树中。

或者,如果您在 <ui:repeat var="property"> 中拥有这一切,并且您使用的是 Mojarra,则至少升级到 2.1.29 或 2.2.7,其中已修复此状态保存错误。

另请参阅:

  • JSTL in JSF2 Facelets... makes sense?
  • PropertyNotFoundException on conditionally rendered subclasses in ui:repeat