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

标签 spring-boot thymeleaf flash-message

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

我正在尝试实现 link 中提出的解决方案但它并不打算在介绍中解释的那样在 Spring-Boot 上工作。

最佳答案

如解释 here , 将 RedirectAttributes 对象注入(inject)到您的 Controller 中,然后在该对象上调用 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>

关于spring-boot - 如何使用 Thymeleaf 在 Spring Boot 中设置 Flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43751860/

相关文章:

hibernate - Spring JPA 审计因多个数据源而失败

java - 如何使用@SpringBootTest验证一个作业是否运行另一个作业

javascript - node.js express js flash 消息 ajax

java - 无法使用 Spring Boot 在 RabbitMQ 中返回消息

linux - SpringBoot 应用程序在一小时后停止。

java - 在 thymeleaf 中使用迭代变量

java - Spring Boot 表单模型

java - 为 HTTP Post 创建多个对象

ruby-on-rails - 设计 5 : How do I modify the Flash messages from Devise to match my css for flash messages?

php - 登录/注销后显示闪现消息