SpringMVC视图解析,就是将业务数据绑定给JSP域对象,并在客户端进行显示。
域对象:
pageContext、request、session、application
业务数据绑定是有ViewResolver 完成的;
SpringMVC提供了一下几种方式绑定业务数据:
1、Map
2、Model
3、ModelAndView
4、@sessionAttribute
5、@ModelAttribute
- 业务数据绑定到request域对象
Map
SpringMVC在内部使用Model接口存储业务数据,在调用业务方法之前会创建一个隐含对象作为业务数据的存储容器。设置业务方法的入参为Map类型,SpringMVC会将隐含对象的引用传递给入参。
开发者可以对模型中所有数据进行管理,包括访问和添加、修改、删除。
开发者只需要在业务方法处添加Map类型的入参,方法体中便可以通过对入参的操作来完成业务数据的添加。
@RequestMapping("/map") public String mapTest(Map<String, User> map){ User user = new User(); user.setId(7); user.setName("cronaldo"); user.setAge(35); map.put("user", user); return "show"; }
访问http://localhost:7777/view/map
Model
与Map类似,业务方法通过入参来完成业务数据的绑定。
@RequestMapping("/model") public String modelTest(Model model){ User user = new User(); user.setId(10); user.setName("meisi"); user.setAge(33); model.addAttribute("user", user); return "show"; }
访问http://localhost:7777/view/model
HTTPServletRequest
@RequestMapping("/request") public String request(HttpServletRequest request){ User user = new User(); user.setId(8); user.setName("kaka"); user.setAge(39); request.setAttribute("user", user); return "show"; }
访问http://localhost:7777/view/request
ModelAndView
与 Map、 Model 不同的是, ModelAndView 不但包含了业务数据,同时也包含了视图信息,将视图和业务数据进行了整合,封装成一个对象,如果使用 ModelAndView 来处理业务数据,业务方法的返回值必须是 ModelAndView 对象。
业务方法中对 ModelAndView 进行两个操作:
1、填充业务数据
2、绑定视图信息
@RequestMapping("/modelAndView") public ModelAndView modelAndView(){ User user = new User(); user.setId(77); user.setName("buFeng"); user.setAge(41); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("user", user); modelAndView.setViewName("show"); return modelAndView; }
访问http://localhost:7777/view/modelAndView
@RequestMapping("/modelAndView2") public ModelAndView modelAndView2(){ User user = new User(); user.setId(77); user.setName("buFeng"); user.setAge(41); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("user", user); View view = new InternalResourceView("/show.jsp"); modelAndView.setView(view); return modelAndView; }
访问http://localhost:7777/view/modelAndView2
@ModelAttribute
1、定义一个方法,该方法用来返回要填充到业务数据中的对象;
2、给该方法添加@ModelAttribute注解,该方法不是业务方法;
@RequestMapping("/modelAttribute") public String modelAttribute(){ return "show"; } @ModelAttribute public User getUser(){ User user = new User(); user.setId(123); user.setName("sunjian"); user.setAge(32); return user; } @ModelAttribute public void getUser2(Model model){ User user = new User(); user.setId(124); user.setName("sunjian3"); user.setAge(33); model.addAttribute("user", user); } @ModelAttribute public void getUser3(Map<String, User> map){ User user = new User(); user.setId(125); user.setName("sunjia4"); user.setAge(34); map.put("user", user); }
访问http://localhost:7777/view/modelAttribute
- 业务数据绑定到session域对象
session
@RequestMapping("/session") public String session(HttpServletRequest request){ User user = new User(); user.setId(9); user.setName("sunjian9"); user.setAge(29); request.getSession().setAttribute("user", user); return "show2"; } @RequestMapping("/session2") public String session2(HttpSession session){ User user = new User(); user.setId(8); user.setName("sunjian10"); user.setAge(28); session.setAttribute("user", user); return "show2"; }
访问http://localhost:7777/view/session
访问http://localhost:7777/view/session2
@SessionAttributes
在类定义处添加@SessionAttributes注解,只要业务方法中向域对象中存入了模型数据,则SpringMVC会自动把它同步到session中。有名称、类型两种方式。
@SessionAttributes(value = "user")
@SessionAttributes(types = User.class)
@RequestMapping("/session3") public ModelAndView session2(){ User user = new User(); user.setId(007); user.setName("sunjian007"); user.setAge(27); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("show2"); modelAndView.addObject("user", user); return modelAndView; }
访问http://localhost:7777/view/session3
完整代码如下:
entity
package com.sunjian.entity; /** * @author sunjian * @date 2020/3/17 14:25 */ public class User { private Integer id; private String name; private Integer age; @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
controller
package com.sunjian.controller; import com.sunjian.entity.Address; import com.sunjian.entity.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.View; import org.springframework.web.servlet.view.InternalResourceView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.Map; /** * @author sunjian * @date 2020/3/17 19:17 */ @Controller @RequestMapping("/view") //@SessionAttributes(value = "user") @SessionAttributes(types = User.class) public class ViewController { @RequestMapping("/map") public String mapTest(Map<String, User> map){ User user = new User(); user.setId(7); user.setName("cronaldo"); user.setAge(35); map.put("user", user); return "show"; } @RequestMapping("/model") public String modelTest(Model model){ User user = new User(); user.setId(10); user.setName("meisi"); user.setAge(33); model.addAttribute("user", user); return "show"; } @RequestMapping("/request") public String request(HttpServletRequest request){ User user = new User(); user.setId(8); user.setName("kaka"); user.setAge(39); request.setAttribute("user", user); return "show"; } @RequestMapping("/modelAndView") public ModelAndView modelAndView(){ User user = new User(); user.setId(77); user.setName("buFeng"); user.setAge(41); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("user", user); modelAndView.setViewName("show"); return modelAndView; } @RequestMapping("/modelAndView2") public ModelAndView modelAndView2(){ User user = new User(); user.setId(77); user.setName("buFeng"); user.setAge(41); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("user", user); View view = new InternalResourceView("/show.jsp"); modelAndView.setView(view); return modelAndView; } @RequestMapping("/modelAttribute") public String modelAttribute(){ return "show"; } @ModelAttribute public User getUser(){ User user = new User(); user.setId(123); user.setName("sunjian"); user.setAge(32); return user; } @ModelAttribute public void getUser2(Model model){ User user = new User(); user.setId(124); user.setName("sunjian3"); user.setAge(33); model.addAttribute("user", user); } @ModelAttribute public void getUser3(Map<String, User> map){ User user = new User(); user.setId(125); user.setName("sunjia4"); user.setAge(34); map.put("user", user); } @RequestMapping("/session") public String session(HttpServletRequest request){ User user = new User(); user.setId(9); user.setName("sunjian9"); user.setAge(29); request.getSession().setAttribute("user", user); return "show2"; } @RequestMapping("/session2") public String session2(HttpSession session){ User user = new User(); user.setId(8); user.setName("sunjian10"); user.setAge(28); session.setAttribute("user", user); return "show2"; } @RequestMapping("/session3") public ModelAndView session2(){ User user = new User(); user.setId(007); user.setName("sunjian007"); user.setAge(27); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("show2"); modelAndView.addObject("user", user); return modelAndView; } }
show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> ${user} </body> </html>
show2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> ${sessionScope.user} </body> </html>
OK.