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";
}
結果:

