设置ModelAndView对象 , 根据view的名称 , 和视图解析器跳到指定的页面 .
页面 : {视图解析器前缀} + viewName +{视图解析器后缀}
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)2.ServletAPI
通过设置ServletAPI , 不需要视图解析器 .
- 通过HttpServletResponse进行输出
rsp.getWriter().println("Hello,Spring BY servlet API");
- 通过HttpServletResponse实现重定向
rsp.sendRedirect("/index.jsp");
- 通过HttpServletResponse实现转发
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
3.SpringMVC转发和重定向
@RequestMapping("")
public String forward(){
//转发
return "forward:/index.jsp";
}
@RequestMapping("")
public String redirect(){
//重定向
return "redirect:/index.jsp";
}
4.数据处理
1、提交的域名称和处理方法的参数名一致
提交数据 : http://localhost:8080/hello?name=kuangshen
2、提交的域名称和处理方法的参数名不一致
提交数据 : http://localhost:8080/hello?username=kuangshen
@RequestParam("username") : username提交的域的名称 .
3、提交的是一个对象
要求提交的表单域和对象的属性名一致 , 参数使用对象即可
1、实体类
public class User {
private int id;
private String name;
private int age;
//构造
//get/set
//tostring()
}
2、提交数据 : http://localhost:8080/mvc/user?name=kuangshen&id=1&age=15
5.数据显示到前端第一种 : 通过ModelAndView
ModelAndView 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转。
第二种 : 通过ModelMap
ModelMap 继承了 linkedMap ,除了实现了自身的一些方法,同样的继承 linkedMap 的方法和特性;
第三种 : 通过Model
Model 只有寥寥几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解;



