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进阶------数组超详细整理 → |

相关推荐
砍材农夫33 分钟前
spring-ai|Spring‑AI 2.0.0 新特性 + 入门教程
spring boot·spring·spring cloud
小强库计算机毕业设计3 小时前
SpringBoot+Vue3 学生宿舍管理系统实战
java·spring boot·后端·vue·学生宿舍管理系统
Flittly1 天前
【雕虫大技】Agent 动态 Skill 供应链安全加固(三):输出校验与治理闭环实战
java·spring boot·spring
小强库计算机毕业设计1 天前
基于 Spring Boot + Vue3 的校园管理系统
java·spring boot·后端·项目实战·管理系统·技术分享·校园管理系统实战
毕设单片机2 天前
顺顺救助站流浪动物领养系统的设计与实现46649----SpringBoot + Vue.js + MySQL|领养审核、寻宠发布、救助站点与动物论坛协同
java·spring boot·管理系统·流浪动物
云烟成雨TD2 天前
Micrometer 系列【63】统一观测:基于 Spring Boot 的生产级演示案例 | 基于 OTLP 集成 Prometheus + Jaeger
spring boot·云原生·prometheus
csdn2015_2 天前
springboot +mybatis 查询myql开启程序级别缓存
spring boot·缓存·mybatis
chuan.bai2 天前
Java RAG 实战(第 8 篇):Spring Boot RAG 查询 API
java·spring boot·贪心算法
vx-程序开发2 天前
springboot旅游推介平台---附源码24175
java·spring boot·python·spring cloud·eclipse·django·idea
小蒜学长2 天前
“喵汪联盟”宠物领养系统的设计与实现(代码+数据库+LW)
java·spring boot·后端·宠物