0

I want my page to show different image depending on the value in a LinkedHashMap. I have this code:

<c:if test="#{myMap.get(1) == 1}">
     <h:graphicImage name="images/first.png"/>
</c:if>
<c:otherwise>
     <h:graphicImage name="images/second.png"/>
</c:otherwise> 

Now, in my Java class I checked and the value of myMap.get(1) is 1, but the page shows the second image. Where is my error?

edit: I get the exception "EL Expression Unbalanced".

user1418018
  • 189
  • 1
  • 4
  • 17

3 Answers3

0

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.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • Yes, it's ManagedBean. The thing is, it says to me that I have a syntax error in "myMap.get(1) eq 1" for any of the examples – user1418018 Dec 26 '13 at 16:49
-1

I guess you use key type is Integer, Just change to Long,

EL interpret number as Long not Integer.

test="#{myMap.get(1) == 1}" // this line does not work b'coz, EL interpret 1 is Long.

Just try below code.

@ManagedBean(name = "index")
public class IndexManagedBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private final LinkedHashMap<Long, Long> map = new LinkedHashMap<>();

    public IndexManagedBean() {
        map.put(1L, 1L);
        map.put(2L, 2L);
    }

    public LinkedHashMap<Long, Long> getMap() {
        return map;
    }

}


<h:body>
    <c:if test="#{index.map.get(1) == 1}">
        <h:graphicImage name="images/first.png"/>
    </c:if>
    <c:otherwise>
        <h:graphicImage name="images/second.png"/>
    </c:otherwise>
</h:body>
Peerapat A
  • 420
  • 4
  • 13
-1

You have to use $ symbol instead of #.

<c:if test="${myMap.get(1) == 1}">
     <h:graphicImage name="images/first.png"/>
</c:if>

And <c:otherwise> can be used with <c:when> only not with <c:if>.

Sathesh S
  • 1,253
  • 3
  • 23
  • 54
  • The first part of your answer is not right, see [Difference between JSP EL, JSF EL and Unified EL](http://stackoverflow.com/questions/4812755/difference-between-jsp-el-jsf-el-and-unified-el) for an overview. The second part is partially right. We use `` with `` and alongside ``s. – skuntsel Dec 26 '13 at 08:02