• 重定向和转发


    一、ModelAndView

    设置ModelAndView对象,根据view的名称,和视图解析器跳转到指定的页面。

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

    二、ServletAPI

    通过设置ServletAPI ,不需要视图解析器。

    1. 通过HttpServletResponse进行输出
    package com.massimo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.imageio.IIOException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @Controller
    public class ModelTest1 {
    
        @RequestMapping("m1/t1")
        public String test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
            HttpSession session = request.getSession();
            System.out.println(session.getId());
            return "hello";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    效果:
    在这里插入图片描述

    1. 通过HttpServletResponse实现重定向
    package com.massimo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.imageio.IIOException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @Controller
    public class ModelTest1 {
    
        @RequestMapping("/m1/t2")
        public void test2(HttpServletRequest request , HttpServletResponse response) throws Exception{
            response.sendRedirect("/hello");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    效果:
    在这里插入图片描述

    1. 通过HttpServletResponse实现转发
    package com.massimo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.imageio.IIOException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @Controller
    public class ModelTest1 {
        @RequestMapping("m1/t3")
        public void test3(HttpServletRequest request , HttpServletResponse response) throws Exception{
            //转发
            request.setAttribute("msg","/m1/t3");
            request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request,response);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述
    项目结构:
    在这里插入图片描述

    三、通过SpringMVC来实现转发和重定向——无需视图解析器

    沿用上面的项目,并将视图解析器注释掉!

    3.1、方式一

    @RequestMapping("m1/t1")
        public String test1(Model model) {
            //转发
            model.addAttribute("msg","ModelTest1");
            return "/WEB-INF/jsp/hello.jsp";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    效果:
    在这里插入图片描述

    3.2、方式二

    @RequestMapping("m1/t1")
        public String test1(Model model) {
            //转发
            model.addAttribute("msg","ModelTest1+forward");
            return "forward:/WEB-INF/jsp/hello.jsp";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    方式二:
    在这里插入图片描述

    3.3、方式三

    @RequestMapping("m1/t1")
        public String test1(Model model) {
            //重定向
            model.addAttribute("msg","ModelTest1+redirect");
            return "redirect:/index.jsp";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    效果:
    在这里插入图片描述

    四、通过S pringMVC来实现转发和重定向——有视图解析器

    重定向,不需要视图解析器,本质就是重新请求一个新地方。所以注意路径问题。

  • 相关阅读:
    用浏览器进行web应用测试,你会怎么做?
    IDEA调试并运行Spring源码
    搭建CNFS文件系统
    ODrive移植keil(四)—— PWM触发ADC采样
    Unity使用ConfigurableJoint实现物理拖拽物体
    从零开始学Spring Boot系列-集成Kafka
    Python包管理工具之pipenv
    leetcode 637. Average of Levels in Binary Tree 二叉树的层平均值(简单)
    springBoot集成websocket实时消息推送
    JAVAWeb2:整体框架
  • 原文地址:https://blog.csdn.net/Massimo__JAVA/article/details/125535895