SpringMVC学习10:重定向和转发

SpringMVC学习10:重定向和转发

  • 结果跳转方式:

    • 重定向:会改变URL

    • 转发:不会改变URL

 

  • ModelAndView:

    • 设置ModelAndView对象,根据View的名称,和视图解析器跳到指定的页面;

    • 页面:{视图解析器前缀}+viewName+{视图解析器后缀}

      <!--视图解析器-->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/jsp/"/>
         <property name="suffix" value=".jsp"/>
      </bean>
    • 对应的Controller:

      public class ControllerTest1 implements Controller {

         public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
             ModelAndView mv = new ModelAndView();

             mv.addObject("msg","demo");
             mv.setViewName("hello");

             return  mv;
        }
      }

       

  • ServletAPI:通过设置ServletAPI,不需要视图解析器;

    • 1,通过HttpServletResponse进行输出;

    • 2,通过HttpServletResponse实现重定向;

    • 3,通过HttpServletRequest实现转发;

       

    • 测试代码:

      @Controller
      public class ModelTest1 {

         @RequestMapping("/h1")
         public void doTest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
             //重定向
             resp.sendRedirect("a.jsp");
        }

         @RequestMapping("/h2")
         public void doTest2(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
             //转发
             req.setAttribute("msg","我是转发");
             req.getRequestDispatcher("/WEB-INF/jsp/hello1.jsp").forward(req,resp);
        }

         @RequestMapping("/h3")
         public void doTest3(HttpServletRequest req,HttpServletResponse resp) throws IOException {
             //输出
             req.setCharacterEncoding("UTF-8");
             resp.setCharacterEncoding("UTF-8");
             resp.setContentType("text/html");
             resp.getWriter().println("我是输出");
        }
      }

       

 

  • SpringMVC:通过SpringMVC来实现转发和重定向,无需视图解析器;

    • 测试前,需要将视图解析器注释掉;

      @Controller
      public class ModelTest2 {

         @RequestMapping("/t/t1")
         public String test1(){
             //转发
             return "forward:/WEB-INF/jsp/hello1.jsp";
        }

         @RequestMapping("/t/t2")
         public String test2(){
             //重定向
             return "redirect:/a.jsp";
        }

         @RequestMapping("/t/t3")
         public String test3(){
             //转发2
             return "/a.jsp";
        }

      }

       

 

  • SpringMVC:通过SpringMVC来实现转发和重定向,有视图解析器;

    • 测试代码:

      @Controller
      public class ModelTest3 {

         @RequestMapping("/a/t1")
         public String test1(){
             //转发
             return "hello";
        }

         @RequestMapping("/a/t2")
         public String test2(){
             //重定向
             return "redirect:/a.jsp";
        }
      }
    • 重定向,不需要视图解析器,本质就是重新请求一个新地址;只需要注意路径即可;

 

 

 

 

 

 

posted @   gzs1024  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示