JSTL retrieving a hashmap from session

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) 

Answers


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.

See also:

Need Your Help

UITabBarItem selectedImageTintColor. How to find the color to set as tintcolor

ios cocoa-touch uitabbaritem tintcolor

I was using the selectedImageTintColor property in my tabbar controller to change the color of the selected tab bar item.

Filling fixed sized array with either integer 1 or 2 to make a sum X

arrays algorithm sum

Let's say I have a fixed sized array. I want to fill the array with either 1s or 2s so that all element sums up to X.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.