SpringMVC学习笔记04--------结果跳转


1. 请求转发与请求重定向

  1. 请求转发

    • 一次请求
    • 地址栏不会改变
    • 跳转后的代码不会执行
    • 只能在当前项目中转发
    • 可以传递request作用域的信息
  2. 请求重定向

    • 是两次请求
    • 地址栏会改变
    • 跳转后的代码会执行
    • 可以跳转到当前服务器之外的路径
    • 不能把request作用域信息传递下去

2.SpringMVC实现请求转发

以下几种方式的测试我们都采用注解的方式实现,注解相关配置参考

2.1 使用servletApi(不推荐使用)

  1. 编写controller类

    package com.xdw.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Controller
    @RequestMapping("/servlet")
    public class ServletApiController {
    
        @RequestMapping("/forward")
        public void testForward(HttpServletRequest request, HttpServletResponse response) throws Exception {
            request.setAttribute("msg", "I am forward!");
            request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);
        }
    
        @RequestMapping("/redirect")
        public void testRedirect(HttpServletRequest request, HttpServletResponse response) throws Exception {
            // 因为要跳转的jsp页面在WEB-INF目录下,请求重定向无法直接访问到,
            // 我们就先重定向到/servlet/forward,再请求转发至hello.jsp
            response.sendRedirect("/servlet/forward");
        }
    
    }
    
  2. 配置Tomcat,启动并测试
    该种方式采用的是原生的servlet的方式实现的,使用起来比较繁琐,不推荐使用,实际开发中,我们更多的是使用SpringMVC方式进行结果跳转。

2.2 使用SpringMVC实现结果跳转(没有视图解析器的情形下)

  1. 编写springMVC的配置文件applicationContext.xml

     <?xml version="1.0" encoding="UTF-8"?>
     
    
         
         
         
         
         
         
    
    
    
         
     
     
     
     
    
     
    
  2. 编写Controller控制器

    package com.xdw.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/springMVC")
    public class MvcNoVrController {
    
        @RequestMapping("/forward01")
        public String testForward01() {
            // 请求转发: 方式一
            return "/index.jsp";
        }
    
        @RequestMapping("/forward02")
        public String testForward02() {
            // 请求转发: 方式二
            return "forward:/index.jsp";
        }
    
        @RequestMapping("/redirect")
        public String testRedirect() {
            // 请求重定向
            return "redirect:/index.jsp";
        }
    
    }
    
    • 这里因为我们没有配置视图解析器,所以在请求转发的时候需要写全需要跳转的jsp页面(路径加上需要跳转的jsp页面)。
  3. 启动Tomcat测试

2.3 使用SpringMVC实现页面跳转(配置了视图解析器)

  1. 编写SpringMVC配置文件applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    
      
      
      
      
      
      
    
    
    
      
      
          
          
      
    
    
    
  2. 编写controller控制器

      package com.xdw.controller;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
    
      @Controller
      @RequestMapping("/springMVC02")
      public class MvcController {
    
          @RequestMapping("/forward01")
          public String testForward01() {
              // 请求转发: 方式一
              return "index";
          }
    
          @RequestMapping("/forward02")
          public String testForward02() {
              // 请求转发: 方式二
              return "forward:/index.jsp";
          }
    
          @RequestMapping("/redirect")
          public String testRedirect() {
              // 请求重定向
              return "redirect:/index.jsp";
          }
    
      }
    
    • 因为配置了视图解析器,并设置了视图解析器的前缀与后缀,所以请求转发可以直接写成 return "index";,视图解析器拿到这个之后会自动根据我们的配置找到对应的jsp页面!
    • 请求重定向本就是发起另一次请求,所以无论有没有配置视图解析器,都需要写全访问路径(访问资源在项目中的路径 + 资源全称)!

3.测试