简体   繁体   English

您如何使用JSP表达式语言从会话范围之外的HttpSession获取属性?

[英]How do you get an attribute from an HttpSession outside the session scope using the JSP expression language?

Using Jsp expression language I can display a session attribute (a username) on a jsp page stored in the session scope as follows: 使用Jsp表达式语言,我可以在会话范围中存储的jsp页面上显示会话属性(用户名),如下所示:

<c:out value="${sessionScope.username}" />

This prints the 'username' I previously assigned to the session with the setAttribute method correctly. 这将正确打印我之前使用setAttribute方法分配给会话的“用户名”。

However, this does not work when I subsequently assign the HttpSession to a variable. 但是,当我随后将HttpSession分配给变量时,这不起作用。 In the below example requestScope.sessionList returns an ArrayList of HttpSessions. 在下面的示例中,requestScope.sessionList返回HttpSessions的ArrayList。

<c:forEach items="${requestScope.sessionList }" var="se" >
    <c:out value="${se.username }" />
</c:forEach>

The above code will result in a javax.el.PropertyNotFoundException: Property 'username' not found on type org.apache.catalina.session.StandardSessionFacade exception. 上面的代码将导致javax.el.PropertyNotFoundException:在类型org.apache.catalina.session.StandardSessionFacade异常上找不到属性“用户名”。 Can someone explain the proper way to access the 'username' attribute in the above scenario? 有人可以说明在上述情况下访问“用户名”属性的正确方法吗?

I don't think there is a solution. 我认为没有解决方案。 Maybe you could use something like the following. 也许您可以使用类似以下的内容。

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%
    session.setAttribute("cars","vehicles");
    session.setAttribute("toys","playthings");
    session.setAttribute("trees","big plants");
%>
<c:forEach items="${pageContext.session.getAttributeNames()}" var="name" >
   ${name} are ${sessionScope[name]}   <br/> 
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM