Spring中RedirectAttributes参数跳转是一个基于重定向的方案。它把需要传递的参数放在URL中或放在Session中,然后传递到下一个控制器中,达到了控制器之间的传值与跳转。跳转时可以使用内置默认的redirect:或forward:前缀,或者使用URL绝对路径或相对路
Spring中RedirectAttributes参数跳转是一个基于重定向的方案。它把需要传递的参数放在URL中或放在Session中,然后传递到下一个控制器中,达到了控制器之间的传值与跳转。跳转时可以使用内置默认的redirect:或forward:前缀,或者使用URL绝对路径或相对路径来进行跳转。
下面是具体实现步骤。
1. 添加依赖
在项目中的pom.xml文件中添加依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>x.x.x.RELEASE</version>
</dependency>
2. 实现跳转源控制器
在需要跳转的源控制器中添加一个控制器方法,方法中使用RedirectAttributes参数,将需要传递的参数放在RedirectAttributes对象中,然后使用redirect:前缀进行跳转:
@Controller
public class SourceController {
    @GetMapping("/source")
    public String source(RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("message", "This message is from source controller.");
        return "redirect:/destination";
    }
}
上面的示例中,我们使用了@GetMapping注解来标识需要跳转的源控制器方法,同时使用了RedirectAttributes参数。RedirectAttributes实现了org.springframework.ui.Model接口,因此我们可以使用addFlashAttribute()方法向其中添加需要传递的参数。最后,我们使用了redirect:前缀来进行跳转。
3. 实现跳转目标控制器
在跳转目标控制器中实现一个控制器方法,用于接收上一个控制器传递的参数。在方法中使用@RequestParam注解或者从Session中获取参数:
@Controller
public class DestinationController {
    @GetMapping("/destination")
    public String destination(@RequestParam("message") String message, HttpSession session) {
        session.setAttribute("message", message);
        return "destination";
    }
}
上面的示例中,我们使用了@GetMapping注解来标识需要跳转的目标控制器方法,在@RequestParam注解中指定了需要获取的参数名message,并使用了HttpSession对象将其放入Session中。
4. 实现视图层
在Spring Boot项目中resources/static目录下新建一个HTML文件,用于展示从目标控制器中获取到的参数:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Destination Page</title>
</head>
<body>
    <h1>Destination Page</h1>
    <p>${message}</p>
</body>
</html>
5. 测试效果
在浏览器中访问http://localhost:8080/source,可以看到跳转效果,并且在目标控制器所对应的视图层中,展示了从源控制器中传递过来的参数。
下面再给出一些常见的应用场景示例:
示例一:添加成功后跳转到列表页并展示成功信息
在添加成功并保存到数据库后,跳转到列表页,并显示保存成功信息。
源控制器:
@PostMapping("/add")
public String add(@Validated User user, BindingResult result, RedirectAttributes redirectAttributes) {
    if (result.hasErrors()) {
        return "add";
    }
    userRepository.save(user);
    redirectAttributes.addFlashAttribute("message", "User added successfully.");
    return "redirect:/list";
}
目标控制器:
@GetMapping("/list")
public String list(Model model, HttpSession session) {
    String message = (String) session.getAttribute("message");
    if (message != null) {
        model.addAttribute("message", message);
        session.removeAttribute("message");
    }
    model.addAttribute("users", userRepository.findAll());
    return "list";
}
示例二:登录失败后跳转回登录页面并展示错误信息
在登录失败后,跳转回登录页面,并展示错误信息。
源控制器:
@PostMapping("/login")
public String login(@RequestParam("username") String username, @RequestParam("password") String password,
        RedirectAttributes redirectAttributes) {
    // 验证用户名和密码
    if (username.equals("admin") && password.equals("password")) {
        return "redirect:/dashboard";
    } else {
        redirectAttributes.addFlashAttribute("error", "Invalid username or password.");
        return "redirect:/login";
    }
}
目标控制器:
@GetMapping("/login")
public String login(Model model, HttpSession session) {
    String error = (String) session.getAttribute("error");
    if (error != null) {
        model.addAttribute("error", error);
        session.removeAttribute("error");
    }
    return "login";
}
希望这些示例能够帮助您理解Spring RedirectAttributes参数跳转的应用场景和实现方法。
本文标题为:Spring RedirectAttributes参数跳转代码实例
				
        
 
            
        - Java开发学习之Bean的作用域和生命周期详解 2023-01-13
 - SpringBoot Starter机制及整合tomcat的实现详解 2023-05-14
 - 简单实现JSP分页显示效果 2023-07-30
 - JSP实用教程之简易文件上传组件的实现方法(附源码) 2023-08-01
 - Java数据结构之二叉搜索树详解 2022-11-29
 - SpringBoot 整合mybatis+mybatis-plus的详细步骤 2023-01-29
 - Java 方法引用与ambda表达式的联系 2023-01-18
 - Mac上的CLASSPATH,以及Mac如何寻找mysql-connector-java-bin.jar 2023-11-01
 - Spring boot Rabbitmq消息防丢失实践 2023-06-01
 - Go Java算法之外观数列实现方法示例详解 2023-04-07
 
						
						
						
						
						
				
				
				
				