我正在尝试在我的项目上实现闪存消息,并用百里叶spring-boot构建。但是我发现到目前为止,这还不是内置功能。如果是这种情况,则在重定向后向用户显示消息的选项是什么。
我正在尝试实现链接中提出的解决方案,但它并不是要在引言中解释的弹簧靴上工作。
如下所述,将redirectAttribute对象注射到您的控制器中,然后在对象上调用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>
您可以添加此代码,看起来像
<div class="alert alert-success" role="alert"
th:if="${message}">
<span th:text="${message}"></span>
<button aria-label="Close" class="btn-close" data-bs-dismiss="alert"
type="button"></button>
</div>