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

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

  1. 请求转发

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

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

2.SpringMVC实现请求转发

以下几种方式的测试我们都采用注解的方式实现,注解相关配置参考SpringMVC学习笔记02-------第一个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"?>
     <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
             https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
         <!-- 使得dispatcherServlet不处理静态资源 -->
         <mvc:default-servlet-handler/>
         <!-- 配置注解驱动,即配置处理器映射器与处理器适配器 -->
         <mvc:annotation-driven/>
         <!-- 配置扫描包路径,使得指定包下的注解@Controller生效 -->
         <context:component-scan base-package="com.xdw.controller"/>
    
    
    
         <!-- 配置视图解析器 -->
     <!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">-->
     <!--        <property name="prefix" value="/"/>-->
     <!--        <property name="suffix" value=".jsp"/>-->
     <!--    </bean>-->
    
     </beans>
    
  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"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
          https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
      <!-- 使得dispatcherServlet不处理静态资源 -->
      <mvc:default-servlet-handler/>
      <!-- 配置注解驱动,即配置处理器映射器与处理器适配器 -->
      <mvc:annotation-driven/>
      <!-- 配置扫描包路径,使得指定包下的注解@Controller生效 -->
      <context:component-scan base-package="com.xdw.controller"/>
    
    
    
      <!-- 配置视图解析器 -->
      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
          <property name="prefix" value="/"/>
          <property name="suffix" value=".jsp"/>
      </bean>
    
    </beans>
    
  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.测试

posted @   卧龙戏公瑾  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示