tags:

views:

266

answers:

1

The c:if test always fails for me and it never gets inside the loop. I am using the following namespaces

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

The string ('array') to be split is "Tom and Jerry are GAP1 friends"

<s:decorate template="/layout/display-text.xhtml">
    <c:set var="array" value="#{_mybean.value}"/>
    <c:set var="space" value="#{fn:split(array, ' ')}"/>
    <c:set var="len" value="#{fn:length(space)}"/>
    <h:outputText value="total length = #{len}"/><br/>
    <c:forEach begin="0" end="5" var="index">
        <h:outputText value="index = #{index}, value = #{space[index]}"/><br/>
        <c:set var="val" value="#{space[index]}"/>
        <c:if test="#{fn:startsWith(val, 'GAP')}">
            <h:outputText value="Found keyword parameter GAP" /><br/>
        </c:if>
    </c:forEach>
</s:decorate>
+6  A: 

The JSTL core URI is invalid. As per the JSTL TLD it should be (note the extra /jsp):

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

That said, mixing JSF with JSTL is never been a good idea. It won't always give results as you'd expect because they doesn't run in sync as you would expect from the coding. It's more that JSP/JSTL runs from top to bottom first and then hands over the produced result to JSF to process further from top to bottom again. That would cause some specific constructs to fail. Better use pure JSF components/attributes instead.

Instead of c:forEach, rather use Seam's a4j:repeat or Facelets' ui:repeat and instead of c:if make use of the rendered attribute of the JSF component which has to be toggled to show/hide. Instead of all that JSTL c:set, write appropriate code logic in managed bean constructor or action method or getter.

The JSTL functions (fn) taglib is however still highly valuable in JSF. You can keep using it.

BalusC
I have made the following change to my code xmlns:c="http://java.sun.com/jsp/jstl/coreAll the debug statements (h:outputText) print empty values for me. Not sure if all fn: functions are working properly for me here. What else am I missing here?
Kalpana
Read http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets. As BalusC said above, remove c:forEach and c:if, they are being evaluated when the view is built, not when the values are applied to the page.
mtpettyp