Problem with session attributes in JSP EL using Spring MVC
Asked Answered
F

3

9

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.

Using java scriptlet instead, behaves as shown in cases 2 and 3:

  1. <c:out value="${userSession}"/> \\Returns null
  2. <c:out value='<%=request.getSession().getAttribute("userSession")%>'/> \\Works fine - returns session attribute
  3. <c:out value='<%=session.getAttribute("userSession")%>'/> \\Throws exception: cannot find variable session

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.

I'm using Spring MVC 2.5 and JSTL 1.1.2.

I can post anything else needed to clarify the problem.

Fluidextract answered 19/1, 2011 at 1:9 Comment(0)
F
12

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.

It prevents a jsp page from access to session scope variables. So only <%=request.getSession().getAttribute("foo")%> works in that case...

Fluidextract answered 19/1, 2011 at 2:22 Comment(0)
T
9

What does:

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

or

<c:out value="${sessionScope['userSession']}"/>

give?

Tarkany answered 19/1, 2011 at 1:14 Comment(4)
The same with <c:out value="${userSession}"/> : nullFluidextract
Try the sessionScope variable as indicated above.Tarkany
What happens when you set a session var right before you print it: <c:set var="userSession" scope="session" value="Hello"/>?Tarkany
I'm getting the following exception: java.lang.IllegalStateException: Cannot access session scope in page that does not participate in any sessionFluidextract
W
4

<c:out value="${sessionScope}"/> or just ${sessionScope} list all the attributes in portlet application scope.

so, You can try to get your attribut with this expression !

Weisshorn answered 12/2, 2013 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.