SpringMVC中支持資源跳轉,請求轉發、請求重定向都實現了自動拼接應用名,所以有了SpringMVC方式的簡化寫法
1.請求轉發 - 傳統方式
@RequestMapping("/test01.action")
public void test01(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
2.請求轉發 - SpringMVC方式
@RequestMapping("/test02.action")
public String test02(){
return "forward:/index.jsp";
}
3.請求重定向 - 傳統方式
@RequestMapping("/test03.action")
public void test03(HttpServletRequest request,HttpServletResponse response) throws IOException {
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
4.請求重定向 - springmvc方式
@RequestMapping("/test04.action")
public String test04(){
return "redirect:/index.jsp";
}
5.定時刷新 - 傳統方式
SpringMVC沒有提供便捷方式實現定時刷新的功能,只能通過傳統方式實現
@RequestMapping("/test05.action")
public void test05(HttpServletRequest request,HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("註冊成功!3秒後回到主頁!");
response.setHeader("refresh","3;url="+request.getContextPath()+"/index.jsp");
}