当处理请求时,有时请求到不同的资源,那重定向和转发是必不可少的操作。本文将深入探讨重定向和转发的使用方法、区别、适用场景。
本文目录
- 一、转发
- 1. 转发实现
- 2. 转发时如何携带参数
- 3. 转发的特点
- 二、重定向
- 1. 实现重定向
- 2. 重定向时如何携带参数
- 2.1 在URL中拼接参数
- 2.2 使用 RedirectAttributes
- 3. 重定向的特点
- 三、重定向和转发的区别
一、转发
转发是服务器内部的一种操作。当服务器接收到客户端的请求后,会把这个请求直接转发到另一个资源,如JSP、Servlet等进行处理。在这个过程中,客户端对请求被转发这件事毫不知情,其地址栏中的URL也不会发生任何变化。
1. 转发实现
通过返回一个带有 forward:
前缀的字符串来实现转发。
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. 转发时如何携带参数
由于转发是服务器内部操作,请求域中的数据会被保留,所以可以直接在请求域中设置参数。代码如下:
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:
前缀的字符串来实现重定向。代码如下:
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中拼接参数
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
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进阶——数组超详细整理 → |