Spring-mvc 3.0应用程序会话范围 [英] Spring-mvc 3.0 application session scope

查看:155
本文介绍了Spring-mvc 3.0应用程序会话范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



设置会话:

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

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

  HttpSession session = request.getSession(); 
session.getAttribute(application); //应用程序null in href;重定向工作正常

所以我可以在Spring MVC 3中使用应用程序会话范围。这样我就可以通过我的应用程序访问会话。



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

 <! - 作为代理公开的HTTP会话范围的bean  - > 
< bean id =applicationVOclass =com.nypd.viewobjects.ApplicationVOscope =globalSession>
<! - 这个下一个元素会影响周围bean的代理 - >
< aop:scoped-proxy />
< / bean>

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

  @Autowired private ApplicationVO applicationVO; 

我在这里做错了什么?



<我还在控制器上尝试了 @SessionAttribute
@SessionAttributes(applicationVO)但似乎问题仍然存在。



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

解决方案

阅读,但要注意这个问题(具有非单例依赖关系的单例对象)。






或者您可以使用 @SessionAttributes 机制来存储和检索控制器中的任意会话数据。请参阅参考这里



参考:




While using left menu I am not redirecting to the other page but using href to link other pages. But while doing that my session scope which is limited to request no longer exists. So here is my controller code:

Setting the session:

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

Getting the session Object in other controller:

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

So is there any way I can use "application" session scope in Spring MVC 3. So that I can have the access to session through out my application.

I tried this code snippet in my 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> 

I am injecting the object to set and retrieve the simple bean as below:

@Autowired private ApplicationVO applicationVO;

what I am I doing wrong here ?

I also tried @SessionAttribute on the controller @SessionAttributes("applicationVO") but it seems the problem still exists.

I will deeply appreciate if anyone can provide me a small example with two controllers.

解决方案

Read the reference for the defined bean scopes. Here they are:

So what you would usually do is define a bean and register it in scope session. Now you can inject it anywhere you need it. See the explanation here, but beware of this problem (singleton objects with non-singleton dependencies).


Or you can use the @SessionAttributes mechanism to store and retrieve arbitrary session data from your controllers. See the reference here.

Reference:

这篇关于Spring-mvc 3.0应用程序会话范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆