Reputation: 15
I have a session attribute which is an HashMap and representing a shopping cart.
I would like to get the HashMap size in the template to make something like "Shopping cart (4 items)" in the nav bar.
Is there a way to achieve this without adding the HashMap size in every Model of every Controller?
Upvotes: 0
Views: 47
Reputation: 1613
use jstl
funtion along with core tag -
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
Then you can get size
of any map
or list
by this -
<c:set var="cartMapSize" value="${fn:length(yourMap)}"/>
Now you can access cartMapSize
anywhere in your page like this -
${cartMapSize}
In your case this would look like this -
Shopping cart (
${cartMapSize}
items)
Upvotes: 1