5

Consider the following jstl choose:

<c:choose>
    <c:when test="#{AuthMsgBean.rw['2'] ne null}">
        Display Text
    </c:when>

    <c:otherwise>
        <ph:outputText id="pan" value="Component pan could not be created." />
    </c:otherwise>
</c:choose>

AuthMsgBean = Bean

rw = Map

'2' = Key


Question:

When I simply display the #{AuthMsgBean.rw['2'] ne null} value it displays fine (true), but once I try to parse the value to the <c:when test=""/> the when tag re-acts as if the test is always false.

If I put true in the test (test="true") the Display Text is displayed.

Could it be that the <c:when> tag is evaluated before the #{AuthMsgBean.rw['2'] ne null} expression?

If so, is there a workaround?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Koekiebox
  • 5,793
  • 14
  • 53
  • 88
  • if you write `$` instead of `#` it should work: `` - (i use it here in a project some time) – Joergi Jan 14 '13 at 09:42

1 Answers1

13

JSTL and JSF do not run in sync as you'd expect from coding. During JSF view build time, it's JSTL which runs from top to bottom first to produce a view with only JSF tags. During JSF view render time, it's JSF which runs from top to bottom again to produce a bunch of HTML.

Apparently the #{AuthMsgBean} is not present in the scope when it's JSTL's turn to run. That can happen when the tags are placed inside a JSF iterating component such as <h:dataTable>.

Regardless, you do not want to use JSTL here. Make use of the JSF rendered attribtue.

<h:panelGroup rendered="#{not empty AuthMsgBean.rw['2']}">
    Display Text
</h:panelGroup>
<h:outputText id="pan" value="Component pan could not be created." rendered="#{empty AuthMsgBean.rw['2']}" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I want to make a generic component using a tag (as in file.tag). Would this also be a problem with dataTable? – Koekiebox Sep 15 '11 at 22:09
  • 1
    Not if it's a Facelets tag or a JSF (composite) component, but you're lost with "plain vanilla" JSP tags. If you're using Facelets, check this: http://stackoverflow.com/questions/6822000/when-to-use-uiinclude-tag-files-composite-components-and-or-custom-component – BalusC Sep 15 '11 at 22:11
  • Nice answer. Personally I like this component in some cases: http://fractalsoft.net/primeext-showcase-mojarra/sections/utils/switch.jsf – Karl Kildén May 27 '13 at 10:27