我正在尝试在我的项目中使用 Thymeleaf 构建基于 Spring-Boot 的 Flash-Messages。但我发现到目前为止它还不是内置功能。如果是这种情况,重定向后向用户显示消息的选项有哪些。
我正在尝试实现 link 中提出的解决方案但它并不打算在介绍中解释的那样在 Spring-Boot 上工作。
最佳答案
如解释 here , 将 RedirectAttributes 对象注入(inject)到您的 Controller 中,然后在该对象上调用 setFlashAttribute。例如
@GetMapping("/test1")
public String test1(RedirectAttributes redirAttrs){
redirAttrs.addFlashAttribute("message", "This is message from flash");
return "redirect:/test2";
}
@GetMapping("/test2")
public String test2(Model model){
return "test2";
}
该消息现在在“/test2”模型中可用,因此在 test.html 中显示消息,例如
<h2 th:text="${message}"></h2>
关于spring-boot - 如何使用 Thymeleaf 在 Spring Boot 中设置 Flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43751860/