Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional check in c:if always fails

Tags:

java

jsp

jsf

jstl

seam

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>
like image 530
Achaius Avatar asked Dec 05 '25 10:12

Achaius


2 Answers

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.

like image 63
BalusC Avatar answered Dec 07 '25 02:12

BalusC


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.

like image 45
BalusC Avatar answered Dec 07 '25 01:12

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!