Your issue is that <c:if>
is a simple JSTL tag to handle a if something is true, add the following components to JSF component tree situation. On the other hand there is also an if-else tag in JSTL andf that is the <c:choose>
tag that's used in conjunction with <c:when>
and <c:otherwise>
.
So, the right conditional code will be either
<c:choose>
<c:when test="#{myMap.get(1) eq 1}">
<h:graphicImage name="images/first.png"/>
</c:when>
<c:otherwise>
<h:graphicImage name="images/second.png"/>
</c:otherwise>
</c:choose>
or
<c:if test="#{myMap.get(1) eq 1}">
<h:graphicImage name="images/first.png"/>
</c:if>
<c:if test="#{myMap.get(1) ne 1}">
<h:graphicImage name="images/second.png"/>
</c:if>
In case your code is properly configured that's all you need to change. Regarding the other issues you might have faced, you need to clarify the details of your code, i.e. in case you've got the error, post the stacktrace alongside the java code that you thing have produced it.
As of now, we've got no idea what myMap
is, or refers to. For it to be working it should be either @ManagedBean ... class MyMap implements Map ...
, or set via <c:set>
as for instance myBean.myMap
for a bean property of map type, or a scoped <c:forEach>
variable. If it is an render-time variable from <ui:repeat>
, <h:dataTable>
, etc. then it will indeed not work as you expect. See JSTL in JSF2 Facelets... makes sense? for an overview.
The last point of my answer will be to use the rendered
attribute of <h:graphicImage>
tag so that your code does not mess JSTL tags and JSF components, and is a cleaner approach for a plain JSF conditionally-rendered components:
<h:graphicImage name="images/first.png" rendered="#{myMap.get(1) eq 1}" />
<h:graphicImage name="images/first.png" rendered="#{myMap.get(1) ne 1}" />
Provided you understand what myMap
is, you'll be able to get either of the three code snippets working.