java - 自动填充集

标签 java spring hibernate auto-populate

除了Set之外,还有类似AutoPopulatedList的东西吗? 我想要显示的数据是使用 Set 的关联。

public class Employer implements java.io.Serializable {
     private Set<Employee> employees = new HashSet();
}

我尝试使用AutoPopulatedList,但在这种情况下,我必须在 hibernate 中使用List,这需要我使用list-index指定Employee.employeeId 并且每当我稍后通过 Employee 检索 employees 时,列表中的元素之间都会有空格(null 元素)取决于 Employee.employeeId

我需要自动填充集合,因为我需要在创建Employer 时动态生成employees。 当我使用普通 Set 时,我得到以下结果: org.springframework.beans.InvalidPropertyException:bean 类 [model.Employer] 的无效属性“employees[0]”:无法从大小为 0 的 Set 中获取索引为 0 的元素,使用属性路径“employees[0]”访问

还有其他解决办法吗?

编辑

我正在尝试实现动态表单

最佳答案

您不能使用Set作为 MVC 中的绑定(bind)目标,因为无法为其项目创建属性路径。

你应该使用什么

您应该使用Map<Integer, YourType>构建动态表单时。我们已经实现了很多次(所以我知道它有效)是这样的:

  • 使用简单的数字序列作为键,与实际项目没有任何联系
  • 按键序列始终递增,但不需要连续(例如,如果用户删除第二个项目,您最终将得到 1, 3, 4, ... )
  • 如果您想添加其他项目,只需找到最大的数字,然后添加索引为 maxIndex + 1 的表单即可。 (始终递增序列)
  • Map实现必须是 LinkedHashMap 的实例以便保留迭代顺序(如果需要自动填充 Map 字段,则 Spring 默认创建此实现)
  • Map必须是某个父表单对象的一部分(即您不能将 Map 作为顶级表单对象),以便 Spring 能够从属性 getter 推断通用类型

View 和 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 - 自动填充集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002027/

相关文章:

java - JPA - EntityManager 查找方法返回重复项

java - 一个 Portlet 中的多个页面

java - Play Framework 注解顺序

java - 关于 java.util.z iip.* 的使用的一些疑问

java - 使用支持灵活方法签名的 Spring 创建自定义注释

java - 访问 GAE DB 给出 AccessControlException

java - 有没有办法找出哪些 bundle 正在使用我的 bundle ?

java - hibernate 保存到数据库 "Duplicate entry ' 1' for key ' UK_2n5xttsxwbyc6x67l0x8phfwn'"

java - 在数据源中创建不需要的空表

java - Hibernate 的 load() 方法上下文中的代理是什么?