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";
    }
}

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

3. 重定向的特点

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

三、重定向和转发的区别

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

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

相关推荐
北风toto1 小时前
Spring Boot 常用注解 · 概要笔记
spring boot·笔记·后端
宋冠巡1 小时前
Spring Boot 4 + OpenTelemetry + Spring Cloud Gateway:全链路 TraceId 传播验证
spring boot·可观测性·traceid
下一站春风里2 小时前
Spring IoC 控制反转 完整技术笔记
java·spring boot·笔记·spring
小码哥0682 小时前
Spring Boot在线租房系统源码:UniApp找房、VR展示与预约管理落地解析
spring boot·租房小程序·租房系统·在线租房
芜名氏3 小时前
API 文档生成工具——SpringDoc
java·spring boot·ci/cd·设计规范
乌暮1 天前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
小荷才露尖尖角,早有蜻蜓立上头1 天前
SpringBoot+QQ邮箱发送邮件
spring boot
头发还在的女程序员1 天前
Java在线租房系统源码解析:Spring Boot+Vue 3+UniApp房源管理与预约看房方案
java·vue.js·spring boot·租房小程序·租房系统·租房软件
FYKJ_20101 天前
springboot同人周边订单管理系统48955-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·django·php·课程设计
yuyuxun12 天前
协同过滤的校园二手物品智能推荐系统的设计与实现 毕业设计-附源码40082
javascript·vue.js·spring boot·python·数据分析·django·课程设计