Spring boo重定向
时间: 2024-06-08 19:08:40 浏览: 14
在 Spring Boot 中,可以使用 `RedirectView` 或 `RedirectAttributes` 实现重定向。
1. 使用 RedirectView
```java
@GetMapping("/redirect")
public RedirectView redirect() {
RedirectView redirectView = new RedirectView();
redirectView.setUrl("https://www.baidu.com");
return redirectView;
}
```
2. 使用 RedirectAttributes
```java
@GetMapping("/redirect")
public String redirect(RedirectAttributes attributes) {
attributes.addAttribute("param1", "value1");
attributes.addAttribute("param2", "value2");
return "redirect:/target";
}
@GetMapping("/target")
public String target(@RequestParam String param1, @RequestParam String param2) {
// 处理参数
return "target";
}
```
在第二种方式中,RedirectAttributes 用于存储重定向时需要传递的参数,这些参数会被自动拼接到重定向 URL 中,最终重定向到目标页面。在目标页面中,可以通过 `@RequestParam` 注解获取这些参数的值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)