1 Spring MVC 模型数据解析
JSP 四大作用域对应的内置对象:pageContext、request、session、application。 (模型数据解析:将模型数据绑定到域对象里边,然后将域对象传给JSP,然后再到JSP里面进行解析)
模型数据的绑定是由 ViewResolver 来完成,实际开发中,我们需要先添加模型数据,再交给 ViewResolver 来绑定。
Spring MVC 提供了以下几种方式添加数据模型:
- Map
- Model
- ModelAndView
- @SessionAttribute
- @ModelAttribute
?
2 将模型数据绑定到 request
2.1 Map
@Controller
@RequestMapping("/view")
public class viewHandler {
@RequestMapping("/map")
public String map(Map<String,User> map){
User user = new User();
user.setId(1L);
user.setName("张三");
map.put("user",user);
return "view";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${requestScope.user}
</body>
</html>
2.2 Model
@RequestMapping("/model")
public String model(Model model){
User user = new User();
user.setId(1L);
user.setName("张三");
model.addAttribute("user",user);
return "view";
}
2.3 ModelAndView
@RequestMapping("/modelAndView")
public ModelAndView modelAndView(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("view");
return modelAndView;
}
@RequestMapping("/modelAndView2")
public ModelAndView modelAndView2(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
View view = new InternalResourceView("/view.jsp");
modelAndView.setView(view);
return modelAndView;
}
@RequestMapping("/modelAndView3")
public ModelAndView modelAndView3(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView("view");
modelAndView.addObject("user",user);
return modelAndView;
}
@RequestMapping("/modelAndView4")
public ModelAndView modelAndView4(){
User user = new User();
user.setId(1L);
user.setName("张三");
View view = new InternalResourceView("/view.jsp");
ModelAndView modelAndView = new ModelAndView(view);
modelAndView.addObject("user",user);
return modelAndView;
}
@RequestMapping("/modelAndView5")
public ModelAndView modelAndView5(){
User user = new User();
user.setId(1L);
user.setName("张三");
View view = new InternalResourceView("/view.jsp");
ModelAndView modelAndView = new ModelAndView(view);
modelAndView.addObject("user",user);
return modelAndView;
}
@RequestMapping("/modelAndView6")
public ModelAndView modelAndView6(){
User user = new User();
user.setId(1L);
user.setName("张三");
Map<String,User> map = new HashMap<>();
map.put("user",user);
ModelAndView modelAndView = new ModelAndView("view",map);
return modelAndView;
}
@RequestMapping("/modelAndView7")
public ModelAndView modelAndView7(){
User user = new User();
user.setId(1L);
user.setName("张三");
Map<String,User> map = new HashMap<>();
map.put("user",user);
View view = new InternalResourceView("/view.jsp");
ModelAndView modelAndView = new ModelAndView(view,map);
return modelAndView;
}
@RequestMapping("/modelAndView8")
public ModelAndView modelAndView8(){
User user = new User();
user.setId(1L);
user.setName("张三");
ModelAndView modelAndView = new ModelAndView("view","user",user);
return modelAndView;
}
@RequestMapping("/modelAndView9")
public ModelAndView modelAndView9(){
User user = new User();
user.setId(1L);
user.setName("张三");
View view = new InternalResourceView("/view.jsp");
ModelAndView modelAndView = new ModelAndView(view,"user",user);
return modelAndView;
}
2.4 HttpServletRequest
@RequestMapping("/request")
public String request(HttpServletRequest request){
User user = new User();
user.setId(1L);
user.setName("张三");
request.setAttribute("user", user);
return "view";
}
2.5 @ModelAttribute
- 定义一个方法,该方法专门用来返回要填充到模型数据中的对象
@ModelAttribute
public User getUser(){
User user = new User();
user.setId(1L);
user.setName("张三");
return user;
}
@ModelAttribute
public void getUser(Map<String,User> map){
User user = new User();
user.setId(1L);
user.setName("张三");
map.put("user",user);
}
@ModelAttribute
public void getUser(Model model){
User user = new User();
user.setId(1L);
user.setName("张三");
model.addAttribute("user",user);
}
- 业务方法中无需再处理模型数据,只需要返回视图即可。
@RequestMapping("modelAttribute")
public String modelAttribute(){
return "view";
}
?
3. 将模型数据绑定到 session 对象
3.1 直接使用原生的 Servlet API
@RequestMapping("/session")
public String session(HttpServletRequest request){
HttpSession session = request.getSession();
User user = new User();
user.setId(1L);
user.setName("张三");
session.setAttribute("user",user);
return "view";
}
@RequestMapping("/session2")
public String session2(HttpSession session){
User user = new User();
user.setId(1L);
user.setName("张三");
session.setAttribute("user",user);
return "view";
}
3.2 @SessionAttribute
@SessionAttributes(value = "user")
@SessionAttributes({"user","address"})
public class viewHandler {
……
}
对于 ViewHandler 中的所有业务方法,只要向 request 中添加了key = “user”、key = “address” 的对象时,Spring MVC 会自动将该数据添加到 session 中,保存 key 不变。
@SessionAttributes({User.class,Address.class})
@SessionAttributes(types = User.class)
public class viewHandler {
……
}
对于 ViewHandler 中的所有业务方法,只要向 request 中添加了数据类型是 User、Address 的对象时,Spring MVC 会自动将该数据添加到 session 中,保存 key 不变。 ?
4 将模型数据绑定到 application 对象
@RequestMapping("/application")
public String application(HttpServletRequest requst){
ServletContext application = requst.getServletContext();
User user = new User();
user.setId(1L);
user.setName("张三");
application.setAttribute("user",user);
return "view";
}
|