SpringMVC实战——转发和重定向及实际场景

当处理请求时,有时请求到不同的资源,那重定向和转发是必不可少的操作。本文将深入探讨重定向和转发的使用方法、区别、适用场景。

本文目录

    • 一、转发
      • [1. 转发实现](#1. 转发实现)
      • [2. 转发时如何携带参数](#2. 转发时如何携带参数)
      • [3. 转发的特点](#3. 转发的特点)
    • 二、重定向
      • [1. 实现重定向](#1. 实现重定向)
      • [2. 重定向时如何携带参数](#2. 重定向时如何携带参数)
        • [2.1 在URL中拼接参数](#2.1 在URL中拼接参数)
        • [2.2 使用 RedirectAttributes](#2.2 使用 RedirectAttributes)
      • [3. 重定向的特点](#3. 重定向的特点)
    • 三、重定向和转发的区别

一、转发

转发是服务器内部的一种操作。当服务器接收到客户端的请求后,会把这个请求直接转发到另一个资源,如JSP、Servlet等进行处理。在这个过程中,客户端对请求被转发这件事毫不知情,其地址栏中的URL也不会发生任何变化。

1. 转发实现

通过返回一个带有 forward: 前缀的字符串来实现转发。

java 复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ForwardController {

    @RequestMapping(value = "/forward", method = RequestMethod.GET)
    public String forward() {
        // 转发到另一个请求处理方法
        return "forward:/target";
    }

    @RequestMapping(value = "/target", method = RequestMethod.GET)
    public String targetPage() {
        return "target";
    }
}

2. 转发时如何携带参数

由于转发是服务器内部操作,请求域中的数据会被保留,所以可以直接在请求域中设置参数。代码如下:

java 复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ForwardWithParamsController {

    @RequestMapping(value = "/forward", method = RequestMethod.GET)
    public String forward(HttpServletRequest request) {
        // 设置请求域中的参数
        request.setAttribute("message", "123");
        return "forward:/target";
    }

    @RequestMapping(value = "/target", method = RequestMethod.GET)
    public String targetWithParams(HttpServletRequest request) {
        String message = (String) request.getAttribute("message");
        System.out.println("接收到的参数: " + message);
        return "target";
    }
}

3. 转发的特点

  • 服务器内部操作
  • 共享请求域
  • 地址栏不变

二、重定向

重定向是服务器向客户端发送一个状态码,通常是302,以及一个新的URL,客户端接收到这些信息后会自动向新的URL发起请求。因此,重定向会使客户端的地址栏发生改变。

1. 实现重定向

可以通过返回一个带有 redirect: 前缀的字符串来实现重定向。代码如下:

java 复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RedirectController {

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String redirect() {
        // 重定向到另一个请求处理方法
        return "redirect:/target";
    }

    @RequestMapping(value = "/target", method = RequestMethod.GET)
    public String target() {
        return "target";
    }
}

2. 重定向时如何携带参数

由于重定向是两次请求,请求域中的数据不会被保留,所以需要采用其他方式携带参数。

2.1 在URL中拼接参数
java 复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RedirectWithParamsController {

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String redirect() {
        String message = "123";
        return "redirect:/target?message=" + message;
    }

    @RequestMapping(value = "/target", method = RequestMethod.GET)
    public String target(String message) {
        System.out.println("接收到的参数: " + message);
        return "target";
    }
}
2.2 使用 RedirectAttributes
java 复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class RedirectWithRedirectAttributesController {

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String redirectWithRedirectAttributes(RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("message", "123");
        return "redirect:/target";
    }

    @RequestMapping(value = "/target", method = RequestMethod.GET)
    public String target() {
        return "target";
    }
}

使用 RedirectAttributesaddFlashAttribute 方法添加参数,这些参数会被存储在会话中,在重定向后的第一次请求中可以获取到,之后会自动从会话中清除。

3. 重定向的特点

  • 客户端重新请求
  • 不共享请求域
  • 地址栏改变

三、重定向和转发的区别

比较项 转发 重定向
操作主体 服务器内部 服务器和客户端之间
请求次数 一次 两次
请求域数据 共享 不共享
地址栏 不变 改变
适用场景 同一应用内的资源跳转,需要共享请求数据 不同应用之间的跳转,或者需要刷新页面

|-----------------------------------------------------------------------------------------------------|--------------------|--------------------------------------------------------------------------------------------------|
| ← 上一篇 Java进阶------常用类及常用方法详解 | 记得点赞、关注、收藏哦! | 下一篇 Java进阶------数组超详细整理 → |

相关推荐
麦兜*20 小时前
Redis数据迁移实战:从自建到云托管(阿里云/腾讯云)的平滑过渡
java·spring boot·redis·spring·spring cloud·阿里云·腾讯云
Roye_ack21 小时前
【项目实战 Day7】springboot + vue 苍穹外卖系统(微信小程序 + 微信登录模块 完结)
spring boot·redis·后端·小程序·个人开发·学习方法·苍穹外卖
往事随风去21 小时前
震惊!Spring Boot中获取真实客户端IP的终极方案,99%的人都没做对!
spring boot·后端
Roye_ack21 小时前
【项目实战 Day5】springboot + vue 苍穹外卖系统(Redis + 店铺经营状态模块 完结)
java·spring boot·redis·学习·mybatis
Q_Q51100828521 小时前
python+nodejs+springboot在线车辆租赁信息管理信息可视化系统
spring boot·python·信息可视化·django·flask·node.js·php
JIngJaneIL21 小时前
记账本|基于SSM的家庭记账本小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·家庭记账本小程序
泉城老铁1 天前
除了群机器人,如何通过钉钉工作通知API给指定用户发消息?
spring boot·后端
泉城老铁1 天前
springboot 对接钉钉发送消息
spring boot·后端
shallwe小威1 天前
SpringBoot集成Kafka
spring boot·后端·kafka
哞哞不熬夜1 天前
JavaEE--SpringBoot
java·spring boot·java-ee