Spring-mvc 3.0应用程序会话范围

sau*_*abh 5 java spring spring-mvc

使用左侧菜单时,我没有重定向到其他页面,而是使用href链接其他页面.但在执行此操作时,我的会话范围仅限于请求不再存在.所以这是我的控制器代码:

设置会话:

request.getSession(true).setAttribute("application", application);
Run Code Online (Sandbox Code Playgroud)

在其他控制器中获取会话对象:

HttpSession session = request.getSession();
session.getAttribute("application"); //application null in href; redirect works fine
Run Code Online (Sandbox Code Playgroud)

那么有什么方法可以在Spring MVC 3中使用"应用程序"会话范围.这样我就可以通过我的应用程序访问会话.

我在application-servlet.xml中尝试了这段代码片段

<!-- a HTTP Session-scoped bean exposed as a proxy --> 
<bean id="applicationVO" class="com.nypd.viewobjects.ApplicationVO" scope="globalSession"> 
<!-- this next element effects the proxying of the surrounding bean --> 
<aop:scoped-proxy/> 
</bean> 
Run Code Online (Sandbox Code Playgroud)

我正在注入对象来设置和检索简单的bean,如下所示:

@Autowired private ApplicationVO applicationVO;
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

我也试过@SessionAttribute控制器, @SessionAttributes("applicationVO")但似乎问题仍然存在.

如果有人能为我提供一个带有两个控制器的小例子,我将深表感谢.

Sea*_*oyd 12

阅读已定义的bean范围参考.他们来了:

bean范围

所以你通常要做的是定义一个bean并在范围内注册它session.现在您可以将它注入任何您需要的地方.请参阅此处说明,但请注意此问题(具有非单例依赖项的单例对象).


或者,您可以使用该@SessionAttributes机制来存储和检索控制器中的任意会话数据.请参阅此处参考.

参考: