如何在Spring MVC控制器之间共享SessionAttributes? [英] How to share SessionAttributes between Controllers in Spring MVC?
问题描述
我想和大家分享会话属性betweet Spring MVC中两个控制器,使用@SessionAttributes注释。
I would like to share Session Attributes betweet two Controllers in Spring MVC, using the @SessionAttributes annotation.
下面是一个简单的code我用测试的属性共享:AController.java
Here is a simple code I use to test the attributes sharing : AController.java
@Controller
@SessionAttributes("myParam")
public class AController {
@RequestMapping(value="/a")
public String handle(Model model){
if(!model.containsAttribute("myParam"))
model.addAttribute("myParam", randomInt());
return "a";
}
private int randomInt(){
return new Random().nextInt(100);
}
}
a.jsp
a.jsp
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<h1>Page A</h1>
<p>Param = ${myParam}</p>
</html>
BController.java
BController.java
@Controller
@SessionAttributes("myParam")
public class BController {
@RequestMapping(value="/b")
public String handle(Model model){
if(!model.containsAttribute("myParam"))
model.addAttribute("myParam", randomInt());
return "b";
}
private int randomInt(){
return new Random().nextInt(100);
}
}
b.jsp
b.jsp
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<h1>Page B</h1>
<p>Param = ${myParam}</p>
我期望的行为是去到/一个URL,myParam将被设置为0和99之间的随机值,则该值将在两个控制器之间共享。
The behaviour I expect is to go to the /a URL, myParam would be set to a random value between 0 and 99, then this value would be shared between the two controllers.
不过,会发生什么情况如下:
我去了/一个URL,myParam被设置为一个值(比方说10)。
然后我去/ B URL,myParam设置为另一个值(比方说20)。
当我回去的/一个URL,myParam值是一个由BController设置(myParam = 20)。
However,what happens is the following: I go to the /a URL, myParam is set to a value (let's say 10). Then i go to the /b URL, myParam is set to another value (let's say 20). When I go back to the /a URL, myParam value is the one set by the BController ( myParam =20).
在两个控制器的方法已被执行,则该值是共享的,但在此之前,每个控制器重新定义一个新的值。
Once the two controllers methods have been executed, the value is shared, but before, each controller redefines a new value.
好像如果控制器从未设置一个值到一个SessionAttribute,它不它是否是由另一控制器设置检测该属性
It seems like if a Controller has never set a value to a SessionAttribute, it does not detect that attribute if it was set by another Controller.
我真的很希望能够共享会话属性悫控制器不使用HttpSession对象和Spring MVC的3个对象坚持。
I really would like to be able to share session attributes betwen controllers without using the HttpSession object and sticking with Spring MVC 3 objects.
我想知道如果我错过了什么或者是否有其他做法在控制器之间的会话共享数据。
I would like to know if I missed something or if there are other practices to share data in a session between controllers.
NB:web应用程序部署一个Tomcat7服务器上
NB : the webapp was deployed on a Tomcat7 server.
推荐答案
在Spring MVC中,这并不意味着@SessionAttributes要共享翻过控制器。
In Spring MVC, @SessionAttributes are not meant to be shared accross Controllers.
我所做的却是创建一个对象(通过一个POJO)的会话范围,即持有我需要存储一个会话的所有值。
What I did instead was to create an object (via a POJO) with the Session scope, that holds all the values I need to store for a Session.
然后我使用JDK动态代理对象自动装配的控制。
I then use the JDK dynamic proxy to Autowire the object to the Controllers.
这篇关于如何在Spring MVC控制器之间共享SessionAttributes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!