简体   繁体   中英

session variable empty in the JSP on first access in a Struts2/Spring application

I am facing a problem with the retrieval of session data in my JSP on first access by the user.

I have a session-scope bean "context" filled with some "profiles" in my Struts action. The URL is configured to be allowed to anonymous users in Spring Security.

On first access to the page, the session is correctly initialized in my action but it is seen as empty when I use it in a struts tag.

When I use java code in the JSP, I can see that the session is correct:

<% System.out.println(((my.bean.SessionBean)session.getAttribute("context")).getProfiles()); %> => OK !!

session: <s:property value="%{#session}"/> => KO (empty)

session: ${session} => KO (empty)

The consequence is that Struts tags fail to retrieve the list of "profiles" in my jsp.

When I refresh the page, it works well. It fails only when the session is initialized on first access. I have checked with the debug mode of Struts, the session variable is empty ({}) the first time.

Redirecting to an additional Struts action on first access solves the problem. But this solution is not ideal for me.

Has anyone an idea of the problem? Why would Struts get an empty session?

thanks

Hava a similar problem once, and the guilty was the page directive that we had in all our JSP:

<%@ page session="false"%>

When session attribute is "false", you cannot access to the session object from a JSP page, or any bean in session scope.

I don't know about OGNL, but the equivalent JSP EL expression to your line of Java code is not ${session} . It's

${sessionScope.context.profiles}

${session} looks for a page/request/session/application attribute named "session".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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