1

I just wanted to demonstrate why we should not be using JSTL tags to a colleague but I got lost not sure why is every thing getting rendered.

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

Results are

true
false
true
false 
Section 1 
Section 2 

I would have thought they would be

true
false
true
false 
Section 1 
Shahzeb
  • 4,745
  • 4
  • 27
  • 40
  • test="false" doesn't seem like an expression. shouldn't it be more like test=${1==0}? My jstl fu is very weak, its been quite sometime, so I may be wrong. – questzen Apr 05 '12 at 06:18
  • Why do you think you shouldn't be using JSTL? – erickson Apr 05 '12 at 06:20
  • @erickson word of god i.e. balusc :) http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense . By the way not saying should never be using it but checking `rendered =...` is better . – Shahzeb Apr 05 '12 at 06:23
  • 1
    @erickson, trying to render complex visual models becomes quite tricky with jstl. (like rendering tables and nested forms). Any thing thing done with jstl can be done more easily with custom tags. It is easy to teach custom tags to new java programmers. – questzen Apr 05 '12 at 06:25
  • 1
    In the context of JSF, I agree it can be tricky to use JSTL correctly. But I disagree that custom tags are easier to use than JSTL; that sounds like NIH syndrome. – erickson Apr 05 '12 at 06:28

1 Answers1

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>

The test="true" and test="false" will always evaluate as boolean true, simply because it are valid and non-null String values.

You likely meant to use test="#{true}" and test="#{false}" instead.

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

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

Another problem is that the XML namespace for JSTL tags is wrong, you're using the one of Facelets 1.x while you're using JSF 2.x. It should be

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

As to using JSTL in JSF, check this answer: JSTL in JSF2 Facelets... makes sense?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Have already checked and just mentioned in my comments before you anwered :) . For my question though what about ` ' – Shahzeb Apr 05 '12 at 06:26
  • I removed `section1.1 and section2.2` from the question but what about `section1 and section2 ` the outputs above showing true or false based of what I expect. – Shahzeb Apr 05 '12 at 06:28
  • using `` is also not stopping `` from being displayed. – Shahzeb Apr 05 '12 at 06:34
  • 1
    The XML namespace of core taglib is wrong, so they would not be parsed at all and interpreted as plain HTML. – BalusC Apr 05 '12 at 12:17
  • I have the right namespace and still if I do it won't enter the condition. – eskalera Jul 18 '13 at 11:16