Saturday, October 16, 2010

JSTL snippets

Simple when you know how. Frustrating and time consuming when you don’t
If/ else
Because of the xml format we can’t have a
<c:if test=”${actionBean.test}”>
</c:if>
<c:else>
</c:else>
Instead we have to use choose, when and otherwise.
e.g.
<c:choose>
<c:when test="${actionBean.test}”>
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>

Looping through lists
Realated to above, I was trying to test if a list was populated.
First attempt was
<c:choose>
<c:when test="${actionBean.result.size>0}">
javax.el.PropertyNotFoundException: The class 'java.util.Collections$Synchronize
dCollection' does not have the property 'size'.
at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:547)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:249)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)
at com.sun.el.parser.AstValue.getValue(AstValue.java:118)
at com.sun.el.parser.AstGreaterThan.getValue(AstGreaterThan.java:41)
Truncated. see log file for complete stacktrace
Solution is to use Not Empty
<c:when test="${not empty actionBean.result}">

No comments: