1. 如果返回值为ModelAndView,在处理方法中,返回null时,默认跳转的视图名称为请求名。跳转结果会根据视图解析器来跳转。
@RequestMapping("/hello.do")
public ModelAndView hello(){
System.out.println("hello================");
return null;
}
跳转结果:
2. 如果返回值为ModelAndView,在处理方法中,指定视图名称,那么将跳转到指定的视图名。跳转结果会根据视图解析器来跳转。-----使用最多
@RequestMapping("/hello.do")
public ModelAndView hello(){
System.out.println("hello================");
return new ModelAndView("index");
}
结果:
3. 返回值为void,在处理方法中,默认跳转的视图名称为请求名。跳转结果会根据视图解析器来跳转。
@RequestMapping("/hello.do")
public void hello(){
System.out.println("hello================");
}
结果:
4. 返回值为void,在处理方法中通过ServletAPI来进行跳转:---不用视图解析器
@RequestMapping("/hello.do")
public void hello(HttpServletRequest req,HttpServletResponse resp) throws Exception{
System.out.println("hello================");
req.getRequestDispatcher("hello.jsp").forward(req, resp);
}
结果:
5. 返回值为String,默认情况下,将会以返回值为视图名通过视图解析器来找到跳转的页面。
@RequestMapping("/hello.do")
public String hello() {
System.out.println("hello================");
return "index";
}
结果:
6.返回值为String,在处理方法中,返回null时,默认跳转的视图名称为请求名。跳转结果会根据视图解析器来跳转。
@RequestMapping("/hello.do")
public String hello() {
System.out.println("hello================");
return null;
}
结果:
7. 返回值为String,为返回值加上前缀”redirect:”或者”forward:”那么将会根据返回值去进行转发或重定向,不使用视图解析器:
@RequestMapping("/hello.do")
public String hello() {
System.out.println("hello================");
return "forward:/index.jsp";
}
结果: