【问题标题】:How to set a Flash Message in Spring Boot with Thymeleaf如何使用 Thymeleaf 在 Spring Boot 中设置 Flash 消息
【发布时间】:2017-09-30 19:14:54
【问题描述】:
我正在尝试在我的基于 Spring-Boot 和 Thymeleaf 的项目中实现 Flash 消息。但我发现到目前为止它还不是内置功能。如果是这种情况,重定向后向用户显示消息的选项有哪些。
我正在尝试实施link 中提出的解决方案,但它并不适用于介绍中所述的 Spring-Boot。
【问题讨论】:
标签:
spring-boot
thymeleaf
flash-message
【解决方案1】:
正如here 解释的那样,将 RedirectAttributes 对象注入您的控制器,然后在该对象上调用 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>