(一)Forward
forward:——表示直接转发,而不会进行拼串。
用法1:
在控制器组件(用于拦截前端请求)中添加以下方法:
@RequestMapping(value="/handle01")
public String handle01(){
System.out.println("handle01..");
return "forward:/hello.jsp";
}
说明:控制器拦截前端handle01
请求,拦截后执行注解下方的handle01()
方法。forward:/hello.jsp
——表示转发到当前项目的hello.jsp
页面。forward:
——表示直接转发,而不会进行拼串。/hello.jsp
——当前项目下的helo.jsp页面。
用法2
同样在控制器组件中添加如下方法:
@RequestMapping(value="/handle02")
public String handle02(){
System.out.println("handle02...");
return "forward:/handle01";
}
说明:forward:/handle01
——表示转发到handle01
,即用法1中的handle01
。并执行用法1中的方法。
(二)redirect
redirect:重定向的路径。
用法1:
在控制器组件中添加如下代码:
@RequestMapping(value="/handle03")
public String handle03(){
System.out.println("handle03...");
return "redirect:/hello.jsp";
}
说明:redirect:/hello.jsp
——重定向到当前项目的hello.jsp页面
用法2
在控制器组件中添加如下代码:
@RequestMapping(value="/handle04")
public String handle04(){
System.out.println("handle04...");
return "redirect:/handle03";
}
说明:redirect:/handle03
——重定向到handle03
。即用法1的handle03
,并执行用法1中handle03中的方法,最终重定向到hello.jsp
页面。
总述:两者用法基本如同。