我想使用@SessionAttributes注释在Spring MVC中的两个控制器之间共享会话属性.
这是一个用于测试属性共享的简单代码: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);
}
}
Run Code Online (Sandbox Code Playgroud)
a.jsp
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<h1>Page A</h1>
<p>Param = ${myParam}</p>
</html>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
b.jsp
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<h1>Page B</h1>
<p>Param = ${myParam}</p>
Run Code Online (Sandbox Code Playgroud)
我期望的行为是转到/ a URL,myParam将被设置为0到99之间的随机值,然后这个值将在两个控制器之间共享. …