Java – condition check C: if always fails
c: If the test always fails to me, it will never enter the loop I use the following namespace
xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:c="http://java.sun.com/jstl/core"
The string to be split ('array ') is "Tom and Jerry are friends of Gap1"
<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>
Solution
The JSTL core URI is invalid According to JSTL, TLD should be (note extra / JSP):
xmlns:c="http://java.sun.com/jsp/jstl/core"
In other words, mixing JSF with JSTL is never a good idea It doesn't always give the results you expect, because they don't run synchronously as the code expects JSP / JSTL first runs from top to bottom, and then sends the generated results to JSF for further processing from top to bottom again This can lead to the failure of some specific structures Better use of pure JSF components / attributes
Instead of C: foreach, use seam's A4J: repeat or facelets' UI: repeat instead of C: if. If you use the rendering properties of JSF components, you must switch show / hide Instead of all JSTL C: sets, write the appropriate code logic in the managed bean constructor or operation method or getter
However, the JSTL function (FN) taglib is still very valuable in JSF You can continue to use it