使用Spring MVC的JSP EL中的会话属性问题 [英] Problem with session attributes in JSP EL using Spring MVC
问题描述
我试图使用JSP EL在jsp页面中显示会话属性"userSession",但它返回null(情况1).但是,请求属性可以正确地显示在EL表达式中.
I'm trying to show a session attribute "userSession" in a jsp page using JSP EL, but it returns null (case 1). Request attributes are shown properly in EL expressions though.
相反,使用Java scriptlet的行为如情况2和3所示:
Using java scriptlet instead, behaves as shown in cases 2 and 3:
-
<c:out value="${userSession}"/>
\\返回空值 -
<c:out value='<%=request.getSession().getAttribute("userSession")%>'/>
\\工作正常-返回会话属性 -
<c:out value='<%=session.getAttribute("userSession")%>'/>
\\抛出异常:找不到变量会话
<c:out value="${userSession}"/>
\\Returns null<c:out value='<%=request.getSession().getAttribute("userSession")%>'/>
\\Works fine - returns session attribute<c:out value='<%=session.getAttribute("userSession")%>'/>
\\Throws exception: cannot find variable session
情况3的异常可能与情况1问题有关,例如由于某种原因无法识别会话变量,然后影响EL表达式.
Exception of case 3 may be related to case 1 problem, something like not recognizing the session variable for some reason and that affecting the EL expression then.
我正在使用Spring MVC 2.5和JSTL 1.1.2.
I'm using Spring MVC 2.5 and JSTL 1.1.2.
我可以张贴其他任何东西来澄清问题.
I can post anything else needed to clarify the problem.
推荐答案
已修复.问题出在我的页面使用的include.jsp中找到了<%@ page session="false" %>
指令,该指令已从该全局位置中删除.
Fixed. Problem was with a <%@ page session="false" %>
directive found in an include.jsp used by my page, which I removed from that global place.
它防止jsp页面访问会话范围变量.因此,只有<%=request.getSession().getAttribute("foo")%>
在这种情况下有效...
It prevents a jsp page from access to session scope variables. So only <%=request.getSession().getAttribute("foo")%>
works in that case...
这篇关于使用Spring MVC的JSP EL中的会话属性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!