0

我有一个存储在会话中的哈希图。hashMap 是映射的映射。

HashMapStoredInSession ={"290" = {text="abc", response="someText"}, "276"={text="xyz", response="random"}};  

我不想使用脚本。但是我被一个脚本卡住了,似乎无法让它工作。任何我出错的建议都会很棒。以下 SCRIPTLET + JSTL 的组合有效

小脚本:

<%     
    Map hMap= (Map)request.getSession().getAttribute("HashMapStoredInSession");   
    pageContext.setAttribute("mapofMaps", hMap);  
%>  

JSTL 代码:

<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>  

现在,如果我尝试用以下替换 scriptlet 代码(JSTL 代码没有变化)

<c:set var="mapofMaps" value ='<c:out value ="<%=request.getSession().getAttribute("HashMapStoredInSession")%>"/>'/>  

我收到以下错误

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) 
4

1 回答 1

3

您可以通过引用它${HashMapStoredInSession}

<c:forEach items="${HashMapStoredInSession}" var="outerMap">             

或者,如果您真的想重命名属性名称,请执行以下操作:

<c:set var="mapofMaps" value="${HashMapStoredInSession}" />  

关键是 EL${}已经在页面、请求、会话和应用程序范围内查找属性。因此,您不需要session.getAttribute()通过scriptlet显式使用。

也可以看看:

于 2011-11-30T19:00:12.017 回答