I have a JSP portlet that needs to display different markup according to the value of a bean’s property which is of an enumeration type
public enum State {
CANCELED, COMPLETED
}
I used the following code to do the switch
<c:choose>
<c:when test="#{item.state == 'COMPLETED'}">
<img src="ok.gif" />
</c:when>
<c:when test="#{item.state == 'CANCELED'}">
<img src="ko.gif" />
</c:when>
</c:choose>
but it doesn’t work. Interestingly enough, it returns false in both cases. The item object (inside an ICEFaces data table) is a backing bean with a State getter+setter property.
I have been told to compare the enumeration to a string and use the == operator, but maybe that’s not the way.
So, my question is: how do I use the <c:when> tag to compare a property to an enumeration value?
Then JSTL indeed doesn’t work. It runs during view build time, not during view render time. Basically you can visualize it as follows: JSTL runs from top to bottom first and then hands over the generated result containing JSF tags only to JSF which in turn runs from top to bottom again. At the moment JSTL encounters the iterated JSF datatable
#{item}, it isnulland thus it will always evaulatefalseand JSF will retrieve neither of those images from JSTL.You want to use a JSF tag instead. I’d suggest
<h:graphicImage>in combination with therenderedattribute.