how to get the size of a HashMap using jstl
I开发者_JAVA技巧s there any way to get the size of a Map using JSTL ? I tried with ${myMap.size} but it's not working..
got the solution: Use jstl functions.
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
then use like this:
<c:if test="${fn:length(myMap) > 1 }">
${myMap.size}
isn't working because it gets translated to myMap.getSize() by the EL parser.
Try:
${myMap.size()}
It should work.
The JSTL length function works on a Collection
but not sure if it will work on a Map
. Might be worth a try.
You can see an example to find the JSTL HashMap size here
We are using HashMap size
method here
${numMap.size()}
If you are trying to just print the count - then you have to use fn
tags. But if you want to process all the items in a map or do something based on the number of items available, try this using only core JSTL tag:
<c:if test="${mapVar != null}"/>
<c:forEach var="mapEntry" items="${mapVar}">
<!-- if there is any entry in the map do something -->
</c:forEach>
</c:if>
The above is equivalent to Java Code
if (maVar != null && mapVar.size() >= 1) {
// do something
}
Yes you cannot print out the size like this, but you can still process information of the items exist.
I prefered this solution because that SUN url errors out so the above worked for me.
精彩评论