• 重定向和转发


    一、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来实现转发和重定向——有视图解析器

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

  • 相关阅读:
    pip install face_recognition 报错的解决
    MySQL通用查询日志 general query log 详解
    Bash变量—数值运算与运算符
    捷诚管理信息系统 SQL注入漏洞复现
    2022 Java最新面试题合集
    斩获 offer 的 Java 面试宝典
    为什么短信验证码要设置有效期?
    Mybatis---从入门到深化
    【免杀前置课——Windows编程】十三、事件与信号量——事件与互斥体区别、操纵信号量实现游戏多开访问控制(附代码)
    【随笔】移动端input type|语义与IOS按键
  • 原文地址:https://blog.csdn.net/Massimo__JAVA/article/details/125535895