• 重定向与转发的几种方式



    web.xml配置:

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <servlet>
            <servlet-name>modeAndViewservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:springmvc-servlet.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>modeAndViewservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    一:ServletAPI(不需视图解析器)

        @RequestMapping("/t1")
        public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
            rsp.sendRedirect("/index.jsp");
        }
        @RequestMapping("/t2")
        public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
            //转发
            req.setAttribute("msg","/result/t3");
            req.getRequestDispatcher("/jsp/test.jsp").forward(req,rsp);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    二:通过SpringMVC来实现转发和重定向-不配置视图解析器情况下

    springMvc.xml:

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:component-scan base-package="com.lmy"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    controller:

    package com.lmy;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class ModeAndViewController {
    
        /**
         * 不配置视图解析器-转发
         * @return
         */
        @RequestMapping("/testForward1")
        public String testForward1(Model model) {
            model.addAttribute("smg","你好");
            return "/jsp/test.jsp";
        }
    
        /**
         * 不配置视图解析器-转发
         * @return
         */
        @RequestMapping("/testForward2")
        public String testForward2(Model model) {
            model.addAttribute("smg","你好,转发");
            return "forward:/jsp/test.jsp";
        }
    
        /**
         * 不配置视图解析器-重定向
         * @return
         */
        @RequestMapping("/testRedirect")
        public String testRedirect() {
    //        model.addAttribute("smg","重定向无法像转发一样携带数据");
            return "redirect:/jsp/test.jsp";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    三:通过SpringMVC来实现转发和重定向-配置视图解析器情况下

    springMvc.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:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:component-scan base-package="com.lmy"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    controller:

    package com.lmy;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class ModeAndViewController {
    
        /**
         * 配置视图解析器-转发
         * @return
         */
        @RequestMapping("/testForward1")
        public String testForward1(Model model) {
            model.addAttribute("smg","你好");
            return "test";
        }
    
        /**
         * 重定向
         * @return
         */
        @RequestMapping("/testRedirect")
        public String testRedirect() {
    //        model.addAttribute("smg","重定向无法像转发一样携带数据");
            return "redirect:/jsp/test.jsp";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    四:ModelAndView-必须配置视图解析器

    设置ModelAndView对象 , 根据view的名称 , 和视图解析器跳到指定的页面 .
    页面 : {视图解析器前缀} + viewName +{视图解析器后缀}

    springMvc.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:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:component-scan base-package="com.lmy"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <property name="prefix" value="/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    controller:

        @RequestMapping("/testModelAndView")
        public ModelAndView testModelAndView() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("smg","你好,ModelAndView 转发");
            modelAndView.setViewName("test");
            return modelAndView;
        }
    
        @RequestMapping("/testModelAndView2")
        public ModelAndView testModelAndView2() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("smg","你好,ModelAndView 重定向");
            modelAndView.setViewName("redirect:/jsp/test.jsp");
            return modelAndView;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    五:总结

    • 重定向 , 不需要视图解析器 , 本质就是重新请求一个新地址 , 所以注意路径是全路径
    • 视图解析器路径拼接方式:
      {视图解析器前缀} + viewName +{视图解析器后缀}
    • 使用ModelAndView对象必须视图解析器
  • 相关阅读:
    ResponseBodyAdvice 获取参数
    图像操作编程:实现图像的旋转、缩放和灰度化
    Spring Boot 如何配置 Hikari 数据库连接池
    react 自定义日历 手把手教你
    【JS】JavaScript入门笔记第五弹之预解析、对象~
    使用Ascend八卡训练报错,len to make them match
    【正点原子STM32连载】第二十章 基本定时器实验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
    Linux Command —— cut / grep /sort /uniq /wc
    【数据结构:排序算法】堆排序(图文详解)
    鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
  • 原文地址:https://blog.csdn.net/qq_41863697/article/details/127802121