四、SpringMVC 结果跳转方式
4.1、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 RestFulController {
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a, @PathVariable int b , Model model){
int result = a + b;
model.addAttribute("msg", "结果为" + result);
return "hello";
}
}
4.2、ServletAPI
通过设置ServletAPI,不需要视图解析器
- 通过HttpServletResponse进行输出
- 通过HttpServletResponse进行重定向
- 通过HttpServletResponse实现转发
@RequestMapping("/test1")
public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().println("hello");
}
@RequestMapping("/test2")
public void test2(HttpServletRequest request, HttpServletResponse response) throws IOException {
//重定向
response.sendRedirect("/index.jsp");
}
@RequestMapping("/test3")
public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//转发
request.setAttribute("msg", "/test3");
request.getRequestDispatcher("/jsp/test1.jsp").forward(request, response);
}
4.3、SpringMVC 重定向和转发
不需要视图解析器
@RequestMapping("/test2")
public String test2(Model model){
//转发
model.addAttribute("msg", "hello");
return "forward:/WEB-INF/jsp/hello.jsp";
}
@RequestMapping("/test2")
public String test2(Model model){
//重定向
model.addAttribute("msg", "hello");
return "redirect:index.jsp";
}
有视图解析器
默认情况下为转发 如果需要重定向 在返回的路径上加上redirect
@RequestMapping("/test2")
public String test2(Model model){
//转发
model.addAttribute("msg", "hello");
return "hello";
}
@RequestMapping("/test2")
public String test2(Model model){
//重定向
model.addAttribute("msg", "hello");
return "redirect:index.jsp"";
}
五、SpringMVC 数据处理
5.1、处理提交数据
-
提交的域名称和处理方法的参数一致
提交数据:http://localhost:8080/hello?name=zhangsan
处理方法:
@GetMapping("/t1") public String test3(String name,Model model){ //接受前端参数 System.out.println(name); //将参数返回前端 model.addAttribute("msg", name); return "hello"; }
-
提交的域名称和处理方法的参数不一致
提交数据:http://localhost:8080/hello?username=zhangsan
处理方法:
// @RequestParam("提交的域名称") @RequestMapping("/hello") public String test2(@RequestParam("username")String name){ System.out.println(name); return "hello"; }
-
提交的是一个对象
要求提交的表单域和对象的属性名一致,参数使用对象即可
实体类:
@Data @NoArgsConstructor @AllArgsConstructor public class User { private int id; private String name; private int age; }
提交数据:http://localhost:8080/hello?name=zhangsan&id=2&age=23
处理方法:
@RequestMapping("/user") public String test2(User user){ System.out.println(user); return "hello"; }
5.2、数据在前端显示
-
通过ModelAndView
@Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("msg", "HelloController"); mv.setViewName("test"); return mv; }
-
通过ModelMap
-
通过Model
@GetMapping("/t1") public String test3(String name,Model model){ //接受前端参数 System.out.println(name); //将参数返回前端 model.addAttribute("msg", name); return "hello"; }
对比:
- Model :只有几个方法只适合用于存储数据,简化了对于Model对象的操作和理解
- ModelMap:继承了LinkedHashMap,除了实现自身的一些方法,同样的集成LinkedHashMap的方法和特性
- ModelAndView:可以在存储数据的同时,进行设置返回的逻辑视图,进行控制显示层的跳转