就像是有什么AutoPopulatingList,但对Set?我要显示的数据是使用的关联Set。
AutoPopulatingList
Set
public class Employer implements java.io.Serializable { private Set<Employee> employees = new HashSet(); }
我已经尝试使用过,AutoPopulatingList但是在这种情况下,我必须List在hibernate状态下使用它,这需要我指定list- indexusing,Employee.employeeId并且每当我检索到employeesthrough时Employee,列表中的null元素之间都会有空格,具体取决于Employee.employeeId。
List
list- index
Employee.employeeId
employees
Employee
null
我需要自动填充集合,因为我需要employees在创建时动态生成Employer。当我使用plain时,得到以下内容Set: org.springframework.beans.InvalidPropertyException: Invalid property 'employees[0]' of bean class [model.Employer]: Cannot get element with index 0 from Set of size 0, accessed using property path 'employees[0]'
Employer
org.springframework.beans.InvalidPropertyException: Invalid property 'employees[0]' of bean class [model.Employer]: Cannot get element with index 0 from Set of size 0, accessed using property path 'employees[0]'
还有其他解决方案吗?
编辑
我正在尝试实现动态表格
您无法Set在MVC中用作绑定目标,因为无法为其项目创建属性路径。
Map<Integer, YourType>构建动态表单时应使用。我们已经实现了很多次(所以我知道它正在工作)是这样的:
Map<Integer, YourType>
1, 3, 4, ...
maxIndex + 1
Map
LinkedHashMap
有很多方法可以使用它。例如,我们有一个特殊的 模板子表单 ,当我们需要动态添加另一个子表单时使用。这种方法可能要复杂一些:
<form:form action="${formUrl}" method="post" modelAttribute="organizationUsersForm"> <%-- ... other fields ... --%> <div id="userSubforms"> <c:forEach items="${organizationUsersForm.users.entrySet()}" var="subformEntry"> <div data-subform-key="${subformEntry.key}"> <spring:nestedPath path="users['${subformEntry.key}']"> <%@ include file="user-subform.jspf" %> </spring:nestedPath> </div> </c:forEach> </div> <button onclick="addSubform(jQuery('#userSubforms'), 'users', 'user', 'userTemplate');">ADD ANOTHER USER</button> <%-- other form fields, submit, etc. --%> </form:form> <div class="hide" data-subform-template="user"> <spring:nestedPath path="userTemplate"> <%@ include file="user-subform.jspf" %> </spring:nestedPath> </div> <script> function addSubform(subformContainer, subformPath, templateName, templatePath) { // Find the sequence number for the new subform var existingSubforms = subformContainer.find("[data-subform-key]"); var subformIndex = (existingSubforms.length != 0) ? parseInt(existingSubforms.last().attr("data-subform-key"), 10) + 1 : 0; // Create new subform based on the template var subform = jQuery('<div data-subform-key="' + subformIndex + '" />'). append(jQuery("[data-subform-template=" + templateName + "]").children().clone(true)); // Don't forget to update field names, identifiers and label targets subform.find("[name]").each(function(node) { this.name = subformPath + "["+ subformIndex +"]." + this.name; }); subform.find("[for^=" + templatePath + "]").each(function(node) { this.htmlFor = this.htmlFor.replace(templatePath + ".", subformPath + "["+ subformIndex +"]."); }); subform.find("[id^=" + templatePath + "]").each(function(node) { this.id = this.id.replace(templatePath + ".", subformPath + "["+ subformIndex +"]."); }); // Add the new subform to the form subformContainer.append(subform); } </script>
现在,您可以问 “用户如何删除子表单” ?如果子表单JSPF包含以下内容,这将非常简单:
<button onclick="jQuery(this).parents('[data-subform-key]').remove();">DELETE USER</button>