问题
I am trying to implement Flash-Messages in my project built on Spring-Boot with Thymeleaf. But I found that it's not a built in feature so far. If this is the case, what are the options to show messages to the user after redirection.
I am trying to implement the solution proposed in the link but it's not intended to work on Spring-Boot as explained in the introduction.
回答1:
As explained here, Inject RedirectAttributes object into your controller, then call setFlashAttribute on the object. e.g.
@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";
}
The message is now available in the model of "/test2" so in test.html, display the message e.g
<h2 th:text="${message}"></h2>
来源:https://stackoverflow.com/questions/43751860/how-to-set-a-flash-message-in-spring-boot-with-thymeleaf