我提前道歉,我是 Thymeleaf 中的 Bootstrap 模式新手,我需要帮助才能在使用 Bootstrap 模式表单更新后显示状态消息。
我正在 Spring Boot 中编写一个应用程序,使用 Thymeleaf 作为我的视图模板。我有一个产品页面,在表格中显示产品列表,并且每个产品都有一个更新按钮。更新按钮以引导模式打开更新表单。
表单提交后,后端会检查所有内容,如果没有错误则更新。然后它发回一条消息,并将其传递到产品视图页面。
一切正常,但我的问题是该消息显示在网址中,而不显示在产品页面上。
在产品html页面中,消息显示如下:
<div th:if="${msg}" th:text="${msg}">message</div>
从控制器,消息作为重定向的属性发送:
String msg = "Product updated";
redirectAttributes.addAttribute("msg", msg);
return "redirect:/product/" + product.getId();
更新表单处于模式中,一旦单击提交按钮,该模式就会关闭。但是当模式关闭时,消息出现在网址中,而不是出现在产品页面上。
如何解决这个问题,并在页面或弹出窗口中正确显示消息?有没有办法真正重新加载页面,以便它考虑新消息并正确显示它?
非常感谢您的帮助!
在您的情况下显示错误消息将是这样的
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
您将在网页上这样做
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<form action="#" th:action="@{/}" th:object="${personForm}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" th:field="*{age}" /></td>
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>
但需要与后端Java程序配合。