JSTLセッションからハッシュマップを取得します
質問
セッションに保存されているハッシュマップがあります。ハッシュマップはマップのマップです。
HashMapStoredInSession ={"290" = {text="abc", response="someText"}, "276"={text="xyz", response="random"}};
スクリプトレットを使いたくありません。しかし、私は1つのスクリプトレットにこだわっており、それを機能させることはできません。私が間違っているという提案は素晴らしいことです。次のScriptlet + JSTL Worksの組み合わせ
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}" />
重要なのは、そのエルです ${}
ページ、リクエスト、セッション、アプリケーションスコープの属性をすでに検索しています。したがって、明示的に使用する必要はありません session.getAttribute()
aによって スクリプトレット.
参照:
所属していません StackOverflow