简体   繁体   English

首次访问Struts2 / Spring应用程序时,JSP中的会话变量为空

[英]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. 我在用户首次访问JSP中检索会话数据时遇到问题。

I have a session-scope bean "context" filled with some "profiles" in my Struts action. 我在Struts动作中有一个会话范围的bean“上下文”,其中填充了一些“配置文件”。 The URL is configured to be allowed to anonymous users in Spring Security. 在Spring Security中,URL被配置为允许匿名用户使用。

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. 首次访问该页面时,该会话已在我的操作中正确初始化,但是当我在struts标记中使用该会话时,该会话被视为空。

When I use java code in the JSP, I can see that the session is correct: 当我在JSP中使用Java代码时,可以看到该会话是正确的:

<% 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. 结果是Struts标记无法在我的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. 我已经使用Struts的调试模式进行了检查,会话变量第一次为空({})。

Redirecting to an additional Struts action on first access solves the problem. 在首次访问时重定向到其他Struts操作可以解决该问题。 But this solution is not ideal for me. 但是这种解决方案对我来说并不理想。

Has anyone an idea of the problem? 有谁知道这个问题吗? Why would Struts get an empty session? 为什么Struts会空会议?

thanks 谢谢

Hava a similar problem once, and the guilty was the page directive that we had in all our JSP: 曾经有过类似的问题,而罪魁祸首就是我们所有JSP中的page指令:

<%@ 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. 当session属性为“ false”时,您将无法从JSP页面或会话范围内的任何bean访问会话对象。

I don't know about OGNL, but the equivalent JSP EL expression to your line of Java code is not ${session} . 我不了解OGNL,但是与您的Java代码行等效的JSP EL表达式不是${session} It's 它的

${sessionScope.context.profiles}

${session} looks for a page/request/session/application attribute named "session". ${session}查找名为“ session”的页面/请求/会话/应用程序属性。

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

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