简体   繁体   中英

JSTL c:choose c:when not working in a JSF page

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?

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM