结果跳转方式
ModelAndView
设置 ModelAndView 对象,根据 view 的名称,和视图解析器跳转到指定的页面
页面:{视图解析器前缀}+ viewName +{视图解析器后缀}
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
对应的 controller 类
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
ServletAPI
通过设置 ServletAPI,不需要视图解析器
- 通过 HttpServletResponse 进行输出
- 通过 HttpServletResponse 实现重定向
- 通过 HttpServletResponse 实现转发
@Controller
public class ResultGo {
@RequestMapping("/result/t1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
//输出
rsp.getWriter().println("Hello,Spring BY servlet API");
}
@RequestMapping("/result/t2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
//重定向
rsp.sendRedirect("/index.jsp");
}
@RequestMapping("/result/t3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
//转发
req.setAttribute("msg","/result/t3");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}
}
SpringMVC
通过 SpringMVC 来实现转发和重定向——无需视图解析器:
测试前,将视图解析器注释掉
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test1(Model model){
//转发
model.addAttribute("msg","ModelTest1");
return "/WEB-INF/jsp/test.jsp";
}
@RequestMapping("/m1/t2")
public String test2(Model model){
//转发 方式二
model.addAttribute("msg","ModelTest2");
return "forward:/WEB-INF/jsp/test.jsp";
}
@RequestMapping("/m1/t3")
public String test3(Model model){
//重定向
model.addAttribute("msg","ModelTest3");
return "redirect:/index.jsp";
}
}
通过 SpringMVC 来实现转发和重定向——有视图解析器:
@Controller
public class ModelTest1 {
@RequestMapping("/m1/t1")
public String test1(Model model){
//转发
model.addAttribute("msg","ModelTest1");
return "test";
}
@RequestMapping("/m1/t2")
public String test2(Model model){
//重定向
model.addAttribute("msg","ModelTest2");
return "redirect:/index.jsp";
}
}
数据处理
处理提交数据
1、提交的域名称和处理方法的参数名一致
提交数据:http://localhost:8080/user/t1?name=mithrandir
处理方法:
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/t1")
public String test1(String name, Model model){
//1. 接收前端参数
System.out.println("接收到前端的参数为"+ name);
//2.将返回的结果传递给前端
model.addAttribute("msg",name);
//3.视图跳转
return "test";
}
}
2、提交的域名称和处理方法的参数名一致
提交数据:http://localhost:8080/user/t1?username=mithrandir
处理方法:
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/t1")
public String test1(@RequestParam("username") String name, Model model){
//1. 接收前端参数
System.out.println("接收到前端的参数为"+ name);
//2.将返回的结果传递给前端
model.addAttribute("msg",name);
//3.视图跳转
return "test";
}
}
3、提交的是一个对象
要求提交的表单域和对象的属性名一致,参数使用对象即可
-
实体类:
@Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private int age; }
-
提交数据:http://localhost:8080/user/t2?id=1&name=mithrandir&age=18
-
处理方法:
@Controller @RequestMapping("/user") public class UserController { //前端接收的是一个对象:id,name,age @GetMapping("/t2") public String test2(User user, Model model){ //1. 接收前端参数 System.out.println("接收到前端的参数为"+ user); //2.将返回的结果传递给前端 model.addAttribute("msg",user); //3.视图跳转 return "test"; } }
说明:如果使用对象的话,前端传递的参数名和对象名必须一致,否则就是null
数据显示到前端
第一种:通过 ModelAndView
之前一直都用的是 ModelAndView,不做过多解释
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
第二种:通过 ModelMap
@GetMapping("/t3")
public String test3(@RequestParam("username") String name,ModelMap map){
//1. 接收前端参数
System.out.println("接收到前端的参数为"+ name);
//2.将返回的结果传递给前端,相当于 req.setAttribute("name",name);
map.addAttribute("msg",name);
//3.视图跳转
return "test";
}
第三种:通过 Model
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping("/t1")
public String test1(@RequestParam("username") String name, Model model){
//1. 接收前端参数
System.out.println("接收到前端的参数为"+ name);
//2.将返回的结果传递给前端,相当于 req.setAttribute("name",name);
model.addAttribute("msg",name);
//3.视图跳转
return "test";
}
}
对比
简单来说,区别是:
Model 只有寥寥几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解;
ModelMap 继承了 LinkedMap ,除了实现了自身的一些方法,同样的继承 LinkedMap 的方法和特性;
ModelAndView 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转。