How to set a Flash Message in Spring Boot with Thymeleaf

删除回忆录丶 提交于 2019-12-08 19:26:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!