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

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

我不想使用脚本。但是我陷入了一个脚本,似乎无法使它起作用。我出错的任何建议都会很棒。 Scriptlet + JSTL的以下组合起作用

Scriptlet:

<%     
    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) 
有帮助吗?

解决方案

您可以通过 ${HashMapStoredInSession}.

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

或者,如果您真的想重命名属性名称,请这样做:

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

关键是El ${} 已经查找页面,请求,会话和应用程序范围中的属性。因此,您无需明确使用 session.getAttribute()Scriptlet.

也可以看看:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top