Java Thymeleaf无法发布对象的集合/列表
我正在用SpringMVC、hibernate和thymeleaf创建一个web应用程序。 我有一个可以管理用户的页面,在这个页面上,您应该能够从组中放置和删除用户。 我使用2个多选择框来完成此操作。 我添加了一个jquery脚本,用于处理用户从一个选择框到另一个选择框的移动。 但当我提交时,我的Group.users对象列表是空的,我没有得到任何异常。 有人有什么建议吗 提前谢谢 编辑 我刚刚发现html标记“option”中的所有thymeleaf属性都没有编译。除th外:每个属性。 很明显,问题出在我的thymeleaf文件中 Thymeleaf/edit.html:Java Thymeleaf无法发布对象的集合/列表,java,hibernate,spring-mvc,thymeleaf,Java,Hibernate,Spring Mvc,Thymeleaf,我正在用SpringMVC、hibernate和thymeleaf创建一个web应用程序。 我有一个可以管理用户的页面,在这个页面上,您应该能够从组中放置和删除用户。 我使用2个多选择框来完成此操作。 我添加了一个jquery脚本,用于处理用户从一个选择框到另一个选择框的移动。 但当我提交时,我的Group.users对象列表是空的,我没有得到任何异常。 有人有什么建议吗 提前谢谢 编辑 我刚刚发现html标记“option”中的所有thymeleaf属性都没有编译。除th外:每个属性。 很明显
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<div th:replace="template :: css"></div>
<title>Edit group</title>
</head>
<body>
<script>
$(document).ready(function() {
$(".clickable").click(function() {
if ($(this).hasClass("selected")) {
$(this).removeClass("selected").addClass("unselected");
$('#userGroupContainer').append(this);
$("option:selected").css("background-color", "red");
} else {
$(this).removeClass("unselected").addClass("selected");
$('#userGroupContainerSelected').append(this);
$("option:selected").css("background-color", "green");
}
});
});
</script>
<div id="bodyWrap">
<div th:replace="template :: logo">Logo</div>
<div th:replace="template :: nav">Nav</div>
<div th:replace="template :: messages">Header</div>
<div id="backGround">
<div id="contentWrap">
<form action="@{edit}"
th:action="@{${#httpServletRequest.servletPath}}"
th:object="${group}" th:method="post">
<h1 th:unless="${group.id}">Add group</h1>
<h1 th:if="${group.id}">Edit group</h1>
<hr />
<div th:replace="template :: messages">Header</div>
<div class="newFile">
<input type="hidden" th:field="*{id}" />
<table class="newFile">
<tr>
<th>Name:</th>
<td><input type="text" size="50" th:field="${group.name}" /></td>
</tr>
<tr>
<th>Description:</th>
<td><textarea th:field="${group.description}"></textarea></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<br /> users <br />
<select multiple="multiple" id="userGroupContainer">
<option th:each="u : ${userNotInGroup}" th:text="${u.displayName}" class="clickable unselected" th:value="${u}" ></option>
</select>
<!-- It's all about the select box under this comment -->
<select multiple="multiple" id="userGroupContainerSelected" th:field="*{users}">
<option th:each="ug, rowStat : ${group.users}" th:text="${ug.displayName}" th:value="${ug}" class="clickable selected">Selected</option>
</select>
<div class="form-actions">
<button th:unless="${group.id}" type="submit">Add</button>
<button th:if="${group.id}" type="submit">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
例子
挑选出来的
我强烈建议不要将数据库实体对象用作Spring模型对象,这会导致应用程序被滥用。相反,您应该创建特定的Spring模型对象,并将需要的数据传输到数据库实体。我还怀疑这就是为什么这对您不起作用的原因,Spring试图将参数users
强制到User
类。我很感谢您的评论,但我的老板希望这样做。我也认为这不是我的问题。我想这是因为百里香叶,因为我的物品清单总是空的。不仅使用我现在使用的方法,而且在我将列表发布到临时列表时也是如此。你还有其他建议吗@乔治里