SpringMVC实现重定向,请求转发,定时刷新功能

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");
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章