I have a hashmap which is stored in a session. The hashMap is a map of maps.
HashMapStoredInSession ={"290" = {text="abc", response="someText"}, "276"={text="xyz", response="random"}};
I dont want to use scriptlets. But I am stuck with one scriptlet and cant seem to make it work. Any suggestions where I am going wrong would be great. The following combination of SCRIPTLET + JSTL works
Scriptlet:
<%
Map hMap= (Map)request.getSession().getAttribute("HashMapStoredInSession");
pageContext.setAttribute("mapofMaps", hMap);
%>
JSTL code:
<c:if test="${param.ID != 'null' && not empty param.ID}">
<c:set var="someID" value="${param.ID}" scope="session"/>
</c:if>
<c:forEach items="${mapofMaps}" var="outerMap">
<c:if test="${outerMap.key == someID}"> // this is the line where exception is thrown when the above scriptlet code is replaced with JSTL below
<c:forEach items="${outerMap.value}" var="innerMap">
<c:if test="${innerMap.key == 'param1'}">
<c:set var="response1" value="${innerMap.value}"/>
</c:if>
<c:if test="${innerMap.key == 'param2'}">
<c:set var="response2" value="${innerMap.value}"/>
</c:if>
</c:forEach>
</c:if>
</c:forEach>
Now if I try to replace scriptlet code with following( with no change in JSTL code)
<c:set var="mapofMaps" value ='<c:out value ="<%=request.getSession().getAttribute("HashMapStoredInSession")%>"/>'/>
I get the following error
An error occurred while evaluating custom action attribute "test" with value "${outerMap.key == someID}":
Unable to find a value for "key" in object of class "java.lang.String" using operator "." (null)
You can just reference it by ${HashMapStoredInSession}
.
<c:forEach items="${HashMapStoredInSession}" var="outerMap">
Or if you really want to rename the attribute name, do so:
<c:set var="mapofMaps" value="${HashMapStoredInSession}" />
The key is that EL ${}
already lookups for attributes in page, request, session and application scopes. So you don't need to explicitly use session.getAttribute()
by a scriptlet.