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

Visual Studio 2008 Installer Project - Custom Actions not firing

windows visual-studio visual-studio-2008 windows-installer

I can't seem to get a custom action working. I might be doing this wrong. Here's what I'm trying to do:

Easiest way to convert IGrouping to IHierarchicalDataSource

asp.net linq hierarchical-data igrouping

I have a list of business objects that I want to display in a menu. I can quickly use LINQ to created nested groups to match the desired structure but have to manually iterate through each to

Apache RewriteRule, - (dash) as substitution

apache mod-rewrite

I often see in PHP MVC applications an Apache RewriteRule that looks like this:

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.