Frage

Ich habe eine Hashmap, die in einer Sitzung gespeichert ist. Die Hashmap ist eine Karte von Karten.

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

Ich möchte keine Drehbücher verwenden. Aber ich bin mit einem Drehbuch festgefahren und kann es nicht zum Laufen bringen. Alle Vorschläge, bei denen ich falsch gehe, wären großartig. Die folgende Kombination von Scriptlet + JSTL funktioniert

Drehbuch:

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

Wenn ich nun versuche, den Scriptlet -Code durch folgende zu ersetzen (ohne Änderung im JSTL -Code)

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

Ich bekomme den folgenden Fehler

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) 
War es hilfreich?

Lösung

Sie können es einfach verweisen ${HashMapStoredInSession}.

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

Oder wenn Sie den Attributnamen wirklich umbenennen möchten, tun Sie dies:

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

Der Schlüssel ist, dass El ${} Bereits suchen nach Attributen auf Seite, Anforderungs-, Sitzungs- und Anwendungsbereiche. Sie müssen also nicht explizit verwenden session.getAttribute() durch eine Drehbuch.

Siehe auch:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top