【发布时间】:2020-01-23 01:21:41
【问题描述】:
实体包含嵌套列表。使用 th:each,我可以看到值显示在表单上,但数据库中没有保存更新,并且确定这是因为指定了不正确的“名称”和/或“id”属性,而 Thymeleaf 没有转码。我如何指定它们?
我尝试使用名称和 ID 的不同组合并遵循文档,但它们都不起作用。仅尝试使用 th:each 循环变量,但这也不起作用。
主要实体: CustomAttributesMap.java
@Entity
public class CustomAttributesMap implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private EntityType entityType;
@ElementCollection
//@ManyToMany
private List<CustomAttributesFieldMap> customAttributesFieldMapList = new ArrayList<>();
// setter and getter code
...
}
第一个子列表: CustomAttributesFieldMap.java
@Entity
public class CustomAttributesFieldMap implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private CustomFieldType fieldType;
@ElementCollection
//@ManyToMany
private List<CustomAttribute> customAttributesList = new ArrayList<>();
// setter and getter methods.
...
}
第二个子列表:
@Entity
public class CustomAttribute implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
private boolean required;
private String value;
// setter and getter methods
....
}
Controller.java
...
@ModelAttribute("customAttributesMapList")
public List<CustomAttributesMap> populateCustomAttributes() {
return customerAttributeMapService.findAll();
}
@PostMapping("/customAttributeCustomization")
public String saveCustomAttributeCustomization(
List<CustomAttributesMap> customAttributesMapList, Model model) {
Logger.getLogger(WebController.class, "Logigng for WebController").error(""
+ "customAttributesMapList " + customAttributesMapList);
for(CustomAttributesMap customAttributesMap: customAttributesMapList) {
customerAttributeMapService.save(customAttributesMap);
}
....
CustomAttributesEdit.html
<form class="user" th:action="@{/customAttributeCustomization}" th:object="${customAttributesMapList}" method="POST">
..
<div class="card p-3 collapse" style="max-width: 50rem;" th:each="customAttributesMap, iterIndex: ${customAttributesMapList}" th:id="${customAttributesMap.entityType}">
..
<div th:each="customAttributeFieldMap, iterStat : ${customAttributesMap.customAttributesFieldMapList}">
...
<table align="left" id="alphaTable" class="table table-sm table-bordered">
<thead>
<tr class="d-flex" id="theadRow">
<th class="col-7 text-center" >Name</th>
<th class="col-3 text-center" >Mandatory</th>
<th class="col-2 text-center">Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="customAttribute, rowStat : ${customAttributeFieldMap.customAttributesList}" class="d-flex" th:id="rowalpha-+${rowStat.index}">
<td class="col-7">
<input class="form-control" type="text" th:value="${customAttribute.name}" th:id="customAttributesMapList+${iterIndex.index}+.customAttributesFieldMapList+${iterStat.index}+.customAttributesList+${rowStat.index}+.name" name="customAttributesMapList[+${iterIndex.index}+].customAttributesFieldMapList[+${iterStat.index}+].customAttributesList[+${rowStat.index}+].name"/>
</td>
....
</table>
</form>
我期望在上面的表格中,customattribute.name 的值需要编辑并保存在数据库中,目前还没有发生。在控制器上调试后,主实体列表(CustomAttributesMapList)没有改变,并且不包含 HTML 表单上的任何用户更新。当然这是由于上面的代码输入字段的名称和 id 不正确,无法正确处理。
更新
好的,我找不到任何有用的答案,因此我将实体展平为具有单个实体 CustomAttrubute,它同时具有 entityType 和 fieldType 属性,并且具有包含 CustomAttribute 实体的 CustomAttributesMap (列表),此地图将是从 web 控制器发送到前端的表单对象。但我想回顾一下是否有人对 Thymeleaf 谜题有答案:)。
【问题讨论】:
-
您的
CustomAttributesMap成员名称为customAttributesFieldMapList,但您的表单字段名称为customAttributesMapList。这些不应该匹配吗? -
@Phil,表单对象是一个CustomAttributesMap列表(称为customAttributesMapList),每个CustomAttributesMap都有一个名为customAttributesFieldMapList的字段(即CustomAttributesFieldMap列表),所以我猜代码是正确的?
标签: java spring-boot thymeleaf