0

セッションに保存されているハッシュマップがあります。hashMap はマップのマップです。

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

スクリプトレットを使用したくありません。しかし、私は 1 つのスクリプトレットで立ち往生しており、それを機能させることができないようです。私が間違っている提案は素晴らしいでしょう。次の 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>  

ここで、スクリプトレット コードを次のコードに置き換えようとすると (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()したがって、スクリプトレットで明示的に使用する必要はありません。

参照:

于 2011-11-30T19:00:12.017 に答える