处理模型数据
2020-02-10 本文已影响0人
桑鱼nicoo
Spring MVC提供了以下几种途径输出模型数据:
ModelAndView
控制器处理方法的返回值是ModelAndView,则其既包含视图信息,也包含模型数据信息
// success.jsp 返回的目标页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Insert title here</title>
</head>
<body>
<h4> success page</h4>
time:${requestScope.time}
time:${time}
</body>
</html>
// index.jsp
<a href="/springmvc/testModelAndView">testModelAndView</a>
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
@Controller
@RequestMapping("/springmvc")
public class TestController {
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
String viewName = "success";
ModelAndView modelAndView = new ModelAndView(viewName);
// 添加模型数据到ModelAndView中
modelAndView.addObject("time",new Date());
return modelAndView;
}
}
Map&Model
Spring MVC 在内部使用了一个org.springframework.ui.Model
接口存储模型数据,具体步骤:
1)SpringMVC在调用方法前会创建一个隐含的数据模型,作为模型数据的存储容器, 成为”隐含模型”
2)如果方法的入参类型为Map或Model,会将隐含模型的引用传递给这些入参。
3)在方法体内,可以通过这个入参对象访问到模型中的所有数据,也可以向模型中添加新的属性数据
Spring Web MVC 提供Model、Map或ModelMap让我们能去暴露渲染视图需要的模型数据。
@Controller
@RequestMapping("/springmvc")
public class TestController {
/**
* 目标方法可以添加Map类型(实际上也可以是Model类型或ModelMap类型)的参数
* @param map
* @return
*/
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map, Model model, ModelMap modelMap){
model.addAttribute("a","aaa");
map.put("names", Arrays.asList("Tom","Jerry","Mike"));
modelMap.put("b","bbbbb");
return "success";
}
}
// success.jsp 返回的目标页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Insert title here</title>
</head>
<body>
<h4> success page</h4>
name:${requestScope.names}<br>
a:${requestScope.a}<br>
b:${requestScope.b}<br>
</body>
</html>
// index.jsp
<a href="/springmvc/testMap">testMap</a>
SessionAttributes
若希望在多个请求之间共用某个模型属性数据,则可以在控制器上标注一个@SessionAttributes,配置需要在session中存放的数据范围,Spring MVC将存放在model中对应的数据暂存到HttpSession 中。
@SessionAttributes只能使用在类定义上。
@SessionAttributes 除了可以通过属性名指定需要放到会话中的属性处,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中
- @SessionAttributes(types=User.class) 会将model中所有类型为 User的属性添加到会话中。
- @SessionAttributes(value={"user1","user2"}) 会将model中属性名为user1和user2的属性添加到会话中。
- @SessionAttributes(types={User.class,Dept.class}) 会将model中所有类型为 User和Dept的属性添加到会话中。
- @SessionAttributes(value={"user1","user2"},types={Dept.class}) 会将model中属性名为user1和user2以及类型为Dept的属性添加到会话中。
value和type之间是并集关系
通过一个例子来说明
public class User {
private String username;
private String password;
private String email;
private int age;
private Address address;
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return address;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", age=" + age +
", address=" + address +
'}';
}
public User(String username,String password,String email,int age){
super();
this.username = username;
this.password = password;
this.email = email;
this.age = age;
}
public User(){}
}
@Controller
@RequestMapping("/springmvc")
@SessionAttributes(value = {"user"},types = {String.class})
public class TestController {
/**
* @SessionAttributes 除了可以通过属性名指定需要放到会话中属性外(使用的是value属性值)
* 还可以通过模式属性的对象类型指定哪些模型属性需要放到会话中(实际上使用的是types属性值)
* 注意,该注解只能放在类的上面,而不能修饰方法
* @param map
* @return
*/
@RequestMapping("/testSessionAttributes")
public String testSessionAttributes(Map<String,Object> map){
User user = new User("Tom","123","aa@aa.com",15);
map.put("user",user);
map.put("school","hhh");
return "success";
}
}
// success.jsp 返回的目标页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>Insert title here</title>
</head>
<body>
<h4> success page</h4>
request user:${requestScope.user}
session user:${sessionScope.user}
request school:${sessionScope.school}
session school:${sessionScope.school}
</body>
</html>
// index.jsp
<a href="/springmvc/testSessionAttributes">testMap</a>