How to get session attribute with a dynamic key in EL?

烂漫一生 提交于 2019-12-05 10:11:38

问题


If I set session like this:

<% 
session.setAttribute("taintedAttribute", "what ever we want");
%>

normally we can get session variable like this in EL

${sessionScope.taintedAttribute }

But how about if I want to do like this

<% 
String name = "taintedAttribute";
//session.setAttribute(name, "what ever we want");
session.getAttribute(name);
%>

Then how can we call it in EL?

Can EL get something like ${sessionScope.---dynamic name ---}?

If I do this:

<c:set var="name" value="taintedAttribute" />
<c:out value="${sessionScope.[name]}"/>

the name will be replaced by taintedAttribute as the same as this line

${sessionScope.taintedAttribute}

Is that possible? How can I do that?


回答1:


<c:set var="name" value="taintedAttribute" />
<c:out value="${sessionScope.[name]}"/>

You were close. Remove the period.

<c:set var="name" value="taintedAttribute" />
<c:out value="${sessionScope[name]}"/>

See also:

  • Our EL wiki page
  • Java EE 6 tutorial - Examples of EL expressions



回答2:


Look at http://www.java2s.com/Code/Java/JSTL/JSTLSetVariablesScope.htm

<c:set var="test" value="Session Level Value"
    scope="session" />
<c:out value="${sessionScope.test}" />


来源:https://stackoverflow.com/questions/8309261/how-to-get-session-attribute-with-a-dynamic-key-in-el

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!