如何使用Thymeleaf在Spring Boot中设置Flash消息

Yaz*_*man 7 flash-message thymeleaf spring-boot

我正在尝试在使用Thymeleaf的Spring-Boot构建的项目中实现Flash消息.但我发现它到目前为止还不是内置功能.如果是这种情况,在重定向后向用户显示消息的选项有哪些.

我正在尝试实现链接中提出的解决方案,但它不打算在Spring-Boot上工作,如引言中所述.

jos*_*hua 11

正如解释在这里,进样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";
}
Run Code Online (Sandbox Code Playgroud)

该消息现在在"/ test2"模型中可用,因此在test.html中,显示消息,例如

<h2 th:text="${message}"></h2>
Run Code Online (Sandbox Code Playgroud)