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

2024-04-23

使用左侧菜单时,我不会重定向到其他页面,而是使用 href 链接其他页面。但是,在这样做时,仅限于请求的会话范围不再存在。 这是我的控制器代码:

设置会话:

request.getSession(true).setAttribute("application", application);

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

HttpSession session = request.getSession();
session.getAttribute("application"); //application null in href; redirect works fine

那么有什么方法可以在 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> 

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

@Autowired private ApplicationVO applicationVO;

我在这里做错了什么?

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

如果有人能为我提供一个带有两个控制器的小例子,我将非常感激。


阅读定义的 bean 范围的参考 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes。他们来了:

所以你通常会做的是定义一个 bean 并将其注册到作用域中session。现在您可以将其注射到任何需要的地方。请参阅解释在这里 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-session,但要小心这个问题 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-sing-prot-interaction(具有非单例依赖性的单例对象)。


或者您可以使用@SessionAttributes从控制器存储和检索任意会话数据的机制。请参阅参考这里 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib.

参考:

  • Bean 范围 > 会话范围 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-session
  • 指定要存储在会话中的属性@SessionAttributes http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring-mvc 3.0应用程序会话范围 的相关文章

随机推荐