Spring MVC提供了以下几种途径输出模型数据:
1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据;
2)Map及Model:处理方法入参为org.springframework.ui.Model、org.springframework.ui.ModelMap或java.util.Map时,处理方法返回时,Map中的数据会自动被添加到模型中;
3)@SessionAttributes:将模型中的某个属性暂存到HttpSeession中,以便多个请求之间可以共享这个属性;
4)@ModelAttribute:方法入参标注该注解后,入参的对象就会放到数据模型中。
@SessionAttributes
1)若希望在多个请求之间共享某个模型属性数据,则可以在控制器类上标注@SessionAttributes,Spring MVC将在模型中对应的属性暂存到HttpSession中。
测试1:
在TestModelData.java中添加方法testSessionAttribute:
1 @RequestMapping("/testSessionAttribute") 2 public String testSessionAttribute(Map<String, Object> map) { 3 Account account = new Account("user1", "pwd123", "2018-01-07", "127.0.0.1"); 4 map.put("account", account); 5 6 System.out.println("testSessionAttribute:"+map); 7 8 return SUCCESS; 9 }
Account.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.dx.springlearn.entities; 2 3 public class Account { 4 private String username; 5 private String password; 6 private String registerDate; 7 private String registerIP; 8 9 public Account() { 10 } 11 12 public Account(String username, String password, String registerDate, String registerIP) { 13 super(); 14 this.username = username; 15 this.password = password; 16 this.registerDate = registerDate; 17 this.registerIP = registerIP; 18 } 19 20 public String getUsername() { 21 return username; 22 } 23 24 public void setUsername(String username) { 25 this.username = username; 26 } 27 28 public String getPassword() { 29 return password; 30 } 31 32 public void setPassword(String password) { 33 this.password = password; 34 } 35 36 public String getRegisterDate() { 37 return registerDate; 38 } 39 40 public void setRegisterDate(String registerDate) { 41 this.registerDate = registerDate; 42 } 43 44 public String getRegisterIP() { 45 return registerIP; 46 } 47 48 public void setRegisterIP(String registerIP) { 49 this.registerIP = registerIP; 50 } 51 52 @Override 53 public String toString() { 54 return "Account [username=" + username + ", password=" + password + ", registerDate=" + registerDate 55 + ", registerIP=" + registerIP + "]"; 56 } 57 58 }
修改index.jsp,添加链接HTML:
<a href="testSessionAttribute">test SessionAttribute</a>
修改/WEB-INF/views/success.jsp,添加HTML脚本:
SUCCESS PAGE<br> testSessionAttribute request:${requestScope.account.username }<br> testSessionAttribute session:${sessionScope.account.username }<br>
此时访问index.jsp,并点击链接后条状到success.jsp也页面显示信息如下:
测试2:
此时默认情况下,并没有把account实体存放到HttpSession中,如何才能实现呢?-------修改TestModelData.java,在TestModelData类上添加注解@SessionAttributes:
1 @SessionAttributes(value = { "account" }) 2 @Controller 3 public class TestModelData { 4 private final String SUCCESS = "success"; 5 6 @RequestMapping("/testSessionAttribute") 7 public String testSessionAttribute(Map<String, Object> map) { 8 Account account = new Account("user1", "pwd123", "2018-01-07", "127.0.0.1"); 9 map.put("account", account); 10 11 System.out.println("testSessionAttribute:" + map); 12 13 return SUCCESS; 14 } 15 }
此时重新测试,跳转到success.jsp页面时,显示结果如下:
说明已经把account实体对象存放到map的同时,也存放到了HttpSession中。
2)@SessionAttributes除了可以通过属性名指定需要放到会话中的属性外,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中。
测试3:
上边是通过指定属性名称的方式将account对象存放到HttpSession中的,实际上我们也可以通过指定对象类型实现把某一类对象存放到HttpSession。
修改TestModelData.java
1 @SessionAttributes(value = { "my_value_key" }, types = { Account.class, Integer.class }) 2 @Controller 3 public class TestModelData { 4 private final String SUCCESS = "success"; 5 6 @RequestMapping("/testSessionAttribute") 7 public String testSessionAttribute(Map<String, Object> map) { 8 Account account = new Account("user1", "pwd123", "2018-01-07", "127.0.0.1"); 9 map.put("account", account); 10 map.put("my_value_key", "my_value"); 11 Integer age = 30; 12 map.put("age", age); 13 Float onlineHours = 129.88f; 14 map.put("onlineHours", onlineHours); 15 16 System.out.println("testSessionAttribute:" + map); 17 18 return SUCCESS; 19 } 20 }
修改/WEB-INF/views/success.jsp页面:
1 SUCCESS PAGE<br> 2 testSessionAttribute request account.username:${requestScope.account.username }<br> 3 testSessionAttribute session account.username:${sessionScope.account.username }<br> 4 <br> 5 testSessionAttribute request my_value_key:${requestScope.my_value_key }<br> 6 testSessionAttribute session my_value_key:${sessionScope.my_value_key }<br> 7 <br> 8 testSessionAttribute request age:${requestScope.age }<br> 9 testSessionAttribute session age:${sessionScope.age }<br> 10 <br> 11 testSessionAttribute request age:${requestScope.onlineHours }<br> 12 testSessionAttribute session age:${sessionScope.onlineHours }<br>
此时访问链接地址,跳转到success.jsp页面后,显示结果: