Friday, 19 April 2013

Getting size of a map in JSTL (it's different in JSP2.1 and JSP 2.2)

Surprised to discover that this doesn't cause problems in JSP

<c:out value="${priceMap.lenght}"/>

tried fixing it to

${priceMap.size}

then realised it gets converted to getSize() and so changed to

${priceMap.size()}

and then get a JSP exception:

org.apache.jasper.JasperException: /WEB-INF/jsp/postform.jsp(31,22)
The function getSize must be used with a prefix when a default
namespace is not specified

I was wondering why, then realised that this works in JSP 2.2
(released in 2009) but not in JSP 2.1 (released 2006)

In JSP 2.1 you must use:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<c:out value="${fn:length(priceMap)}" />

No comments: