java Spring-mvc 3.0 应用会话范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5068478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:16:18  来源:igfitidea点击:

Spring-mvc 3.0 application session scope

javaspringspring-mvc

提问by saurabh

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:

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

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.

那么有什么方法可以在 Spring MVC 3 中使用“应用程序”会话范围。这样我就可以在整个应用程序中访问会话。

I tried this code snippet in my application-servlet.xml

我在 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:

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

@Autowired private ApplicationVO applicationVO;

what I am I doing wrong here ?

我在这里做错了什么?

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

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

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

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

回答by Sean Patrick Floyd

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

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

bean scopes

Bean 作用域

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).

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



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

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

Reference:

参考:

回答by prashant

@Session attribute does not store data in session scope. It stores data in conversation scope which is a scope greater than request but less than session. This scope is internally managed by spring for a conversation(which spans across several requests) and removed once the conversation is finished

@Session 属性不在会话范围内存储数据。它将数据存储在会话范围内,该范围大于请求但小于会话。此范围由 spring 内部管理,用于对话(跨越多个请求)并在对话完成后删除

To store your bean in session scope you will have to declare the requestContextListner in your spring-context.xml which would expose the request to the current thread

要将您的 bean 存储在会话范围内,您必须在 spring-context.xml 中声明 requestContextListner,它将向当前线程公开请求