首页 / JAVA / java – 自动填充集
java – 自动填充集
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 自动填充集,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3426字,纯文字阅读大概需要5分钟。
内容图文
有什么像AutoPopulatingList但是对于Set?
我想要显示的数据是使用Set的关联.
public class Employer implements java.io.Serializable {
private Set<Employee> employees = new HashSet();
}
我尝试过使用AutoPopulatingList但是在这种情况下我必须在hibernate中使用List,这需要我使用Employee.employeeId指定list-index,每当我通过Employee检索员工时,列表将在element(null元素)之间有空格依赖在Employee.employeeId上.
我需要自动填充集合,因为我需要在创建雇主时动态生成员工.
当我使用普通Set时,我得到以下内容:
org.springframework.beans.InvalidPropertyException:bean类[model.Employer]的属性’employees [0]’无效:无法从大小为0的集合中获取索引为0的元素,使用属性路径’employees [0]’访问
还有其他解决方案吗?
编辑
我正在尝试实现动态表单
解决方法:
您不能在MVC中将Set用作绑定目标,因为无法为其项创建属性路径.
你应该用什么
你应该使用Map< Integer,YourType>在构建动态表单时.我们多次实施(所以我知道它有效)是这样的:
>使用简单的数字序列作为键,而不与实际项目进行任何连接
>键序列总是在增加,但不需要连续(例如,如果用户将删除第二项,您将最终得到1,3,4 ……)
>如果你想添加另一个项目,你只需要找到最高的数字,然后添加用maxIndex 1索引的表单(总是增加序列)
> Map实现必须是LinkedHashMap的实例,以便保留迭代顺序(如果Map字段需要自动填充,则Spring默认创建此实现)
> Map必须是某个父表单对象的一部分(即,您不能将Map作为顶部表单对象),以便Spring能够从属性getter推断泛型类型
视图和JavaScript实现示例
有很多方法可以解决这个问题.例如,我们有一个特殊的模板子表单,当我们需要动态添加另一个子表单时使用它.这种方法可能要复杂一些:
<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>
内容总结
以上是互联网集市为您收集整理的java – 自动填充集全部内容,希望文章能够帮你解决java – 自动填充集所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。