0

我只是想向同事演示为什么我们不应该使用 JSTL 标签,但我迷路了,不知道为什么每件事都会被渲染。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jstl/core">    
     <h:outputLabel>#{dummyBean.pageBuild}</h:outputLabel>
     <h:outputLabel>#{dummyBean.pageRerendered}</h:outputLabel>     
     <h:outputLabel>#{dummyBean.pageBuild and !dummyBean.pageRerendered}</h:outputLabel>
     <h:outputLabel>#{dummyBean.pageBuild and dummyBean.pageRerendered}</h:outputLabel>
        <c:if test="#{dummyBean.pageBuild and !dummyBean.pageRerendered}">
             <h:outputLabel value="Section 1"></h:outputLabel>
        </c:if>

        <c:if test="#{dummyBean.pageBuild and dummyBean.pageRerendered}">
            <h:outputLabel value="Section 2"></h:outputLabel>
        </c:if>

</ui:composition>

结果是

true
false
true
false 
Section 1 
Section 2 

我原以为他们会

true
false
true
false 
Section 1 
4

1 回答 1

4
<c:if test="true">
     <h:outputLabel value="Section 1.1"></h:outputLabel>
</c:if>

<c:if test="false">
    <h:outputLabel value="Section 2.2"></h:outputLabel>
</c:if>

test="true"and将test="false"始终评估为 boolean true,仅仅是因为它是有效且非空String值。

您可能打算使用test="#{true}"andtest="#{false}"代替。

<c:if test="#{true}">
     <h:outputLabel value="Section 1.1" />
</c:if>

<c:if test="#{false}">
    <h:outputLabel value="Section 2.2" />
</c:if>

另一个问题是 JSTL 标记的 XML 命名空间是错误的,您使用的是 Facelets 1.x 而您使用的是 JSF 2.x。它应该是

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

至于在 JSF 中使用 JSTL,请查看以下答案:JSTL in JSF2 Facelets... 有意义吗?

于 2012-04-05T06:21:59.673 回答