一、使用框架提供的方法
转发
@RequestMapping({"/","/forward"})
public String test1() {
return "forward:res/index.html";
}
完整的请求路径为:http://域名/虚拟目录/res/index.html,res是静态资源访问前缀。
重定向
@RequestMapping("/redirect")
public String test2() {
return "redirect:res/index.html";
}
完整的请求路径为:http://域名/虚拟目录/res/index.html
以上两种方式都会自动补全虚拟目录
二、使用原生Servlet
转发
@RequestMapping("/forward2")
@ResponseBody
public void test3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("res/index.html").forward(request,response);
}
重定向
@RequestMapping("/redirect2")
@ResponseBody
public void test4(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect(request.getContextPath()+"/res/index.html");
}
注:重定向时需要自己添加虚拟目录