如果希望在多个请求之间共用某个模型属性数据,可以在控制器类上标注一个@SessionAttributes注解。
spring mvc将把模型中对应的属性暂存到 http session中。
之前的ModelAndView是把数据放在了request中,通过request进行数据获取。
Map则是继承了ModelAndView,也是放在了request中。
这次的@SessionAttributes则是把数据放在了http session中,所以放置和取用的方法都需要做出改变。
一、单纯的返回Map
先写出一个方法:
1 2 3 4 5 6 7 8 9 10 11 |
@RequestMapping(value = "/testSessionAttributes") public String testSessionAttributes(Map<String, Object> map) { user user = new user(); user.setId(1); user.setUsername("godlikexie"); user.setPassword("yxin"); map.put("user", user); return "hello"; } |
简单地返回了一个map,这样就是把数据放在了请求域里面。
如果只是单纯的这样做,只能在request域中进行获取,httpsession域中无法进行获取。
为此我们改写一下hello.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hello world!</title> </head> <body> <%@ page isELIgnored="false"%> <h1>hello world!!!!!!</h1> testModelAndView: ${requestScope.username} <br><br> testMap: ${requestScope.names} <br><br> testMap: ${requestScope.user} <br><br> testSessionAttributes: ${sessionScope.user} </body> </html> |
做了两手准备,同时获取request域中的user对象和http session域中的user对象
结果为:
明显httpsession获取不到。
二、加上@SessionAttributes注解
将user对象加入httpsession域中。
注意:@SessionAttributes注解位置在controller类前,修饰类,而不是在方法前。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package dao; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; import pojo.lover; import pojo.user; @SessionAttributes(value="user") @Controller public class testdao { @RequestMapping(value = "/testSessionAttributes") public String testSessionAttributes(Map<String, Object> map) { user user = new user(); user.setId(1); user.setUsername("godlikexie"); user.setPassword("yxin"); map.put("user", user); return "hello"; } } |
@SessionAttributes(value=”user”),就是说我们把name为user的对象放进了http session中。
运行,访问http://localhost:8080/5.4springmvc/testSessionAttributes:
这次能够获取了,可喜可贺。
三、别的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package dao; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; import pojo.lover; import pojo.user; @SessionAttributes(value="user", types=String.class) @Controller public class testdao { @RequestMapping(value = "/testSessionAttributes") public String testSessionAttributes(Map<String, Object> map) { user user = new user(); user.setId(1); user.setUsername("godlikexie"); user.setPassword("yxin"); map.put("user", user); map.put("whoami", "godlikexie"); return "hello"; } } |
增加了属性:types=String.class。
这样,只要我们放进map中的是string类型的对象,@SessionAttributes会自动把这个string对象放进http session中。
改写hello.jsp尝试获取whoami对象。
成功获取了。
四、照这样说,也可以这么做咯?
写成@SessionAttributes(types={String.class, user.class})。
只要我们放进map中的是user类型的对象,@SessionAttributes会自动把这个user对象放进http session中,之后就可以获取了,是这样吗?
没错的确是这样。
只要是user类,就会放在httpsession中。
五、总结
可以把内容放到http session中,存在一定的应用场景。当然操作servlet也可以做到。