1

I have written a code like this:

<ui:repeat var="fstDayWk" value="#{calendarBean.date}">
        <c:choose>
            <c:when test="#{fstDayWk == 'Sun'}">
                <c:set var="fstDayWk" value="7"/>
            </c:when>

            <c:when test="#{fstDayWk =='Mon'}">
                <c:set var="fstDayWk" value="1" />
            </c:when>

            <c:when test="#{fstDayWk =='Tue'}">
                <c:set var="fstDayWk" value="2" />
            </c:when>

            <c:when test="#{fstDayWk =='Wed'}">
                <c:set var="fstDayWk" value="3" />
            </c:when>

            <c:when test="#{fstDayWk =='Thu'}">
                <c:set var="fstDayWk" value="4" />
            </c:when>

            <c:when test="#{fstDayWk =='Fri'}">
                <c:set var="fstDayWk" value="5" />
            </c:when>

            <c:when test="#{fstDayWk =='Sat'}">
                <c:set var="fstDayWk" value="6"/>
            </c:when>
            <c:otherwise>
                <c:set var="fstDayWk" value="1" />
            </c:otherwise>
        </c:choose>
</ui:repeat>

But here the value assignment for fstDayWk is always 1. Why so? How could I set value to some variable depending upon some condition in JSF?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
NDeveloper
  • 3,115
  • 7
  • 23
  • 34

1 Answers1

4

JSTL tags runs during view build time, when the JSF component tree is about to be built/constructed based on JSF tags in the XHTML file. JSF components such as <ui:repeat> in turn runs during view render time, when the built JSF component tree is about to produce HTML code.

So, <c:choose> and <ui:repeat> does not run in sync as you seemed to expect from the coding. In fact, it's first the <c:choose> which runs during view build time and then it's the <ui:repeat> which runs during view render time. At that moment when <c:choose> runs, the #{fstDayWk} which is supposed to be set by <ui:repeat> is not available anywhere in the EL scope and thus the <c:choose> always ends up in the <c:otherwise> condition.

It will work if you use <c:forEach> instead of <ui:repeat>. However, much better would be to use a Map<String, Integer> in the model which maintains a mapping between day of week and its index, so that you don't need the whole ugly <c:choose> block anymore.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • How would that Help with `Map`? Can you please explain me with example? – NDeveloper Jun 11 '13 at 13:14
  • Hard to tell as you didn't show that part of code where you actually need the index of day of week. But something like `

    Index of day of week is #{bean.map[fstDayWk]}

    ` would be possible this way. See also http://stackoverflow.com/tags/el/info
    – BalusC Jun 11 '13 at 13:28