controller请求转发,重定向

了解

转发(forward):浏览器地址不会改变,始终是同一个请求。
重定向(sendRedirect): 浏览器地址会改变,是两个请求。

转发forward

有异常抛出就好了:
跳首页:浏览器的url地址不变.可能会找不到静态文件:

  @GetMapping(value = "/index")
    @ApiOperation("首页")
    public void index(HttpServletRequest request, HttpServletResponse response)  throws Exception  {
   request.getRequestDispatcher("/index.html").forward(request, response);
   }

重定向redirect

controller中返回值为void

 @GetMapping(value = "/index")
    @ApiOperation("首页")
    public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("/index.html");
    }
 第三种方式:controller中返回值为ModelAndView

return new ModelAndView(“redirect:/toList”);

世界上所有的不公平都是由于当事人能力不足造成的.
原文地址:https://www.cnblogs.com/javayida/p/13346794.html