Value expressions still evaluated despite ui:fragment rendered=“false”

◇◆丶佛笑我妖孽 提交于 2019-12-20 07:35:06

问题


I have bean:

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

also have block of code on page:

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

The condition #{property.type eq 'checkbox'} = false

But I get next exception:

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

I expect if the attribute rendered=false in ui:include, then this block will not be processed.


回答1:


<ui:fragment rendered> prevents it from rendering the HTML output, but it doesn't prevent it from ending up in JSF component tree and being eligible for state saving.

Use <c:if test> instead. It runs during view build time instead of view render time and thus the whole bunch won't end up in JSF component tree at all.

Or, if you have this all inside an <ui:repeat var="property">, and you are using Mojarra, then upgrade to at least 2.1.29 or 2.2.7 wherein this state saving bug was fixed.

See also:

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


来源:https://stackoverflow.com/questions/28563356/value-expressions-still-evaluated-despite-uifragment-rendered-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!