使用Spring的动态表单更好的方法?

我想知道在使用SpringMVC和Spring表单时是否有更简单/更好的方法来处理动态表单(通过js向dom添加表单项)?

具有Invoice对象的成像具有许多LineItem。

public class Invocie { private List LineItems; public Invoice() { lineItems = ListUtils.lazyList(new ArrayList(), FactoryUtils.instantiateFactory(LineItem.class)); } } 

显示属于当前使用的发票的项目

  

要添加LineItems,我有一些js计算新索引并将其添加到DOM。 删除LineItem时,我当前必须重新编号所有索引,这是我想避免的部分,是否可能?

我已经实现了一个教程,可以帮助您在客户端使用jQuery解决此问题,并为您创建支持对象的Springs AutoPopulating列表。

http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/

来自Webarchive的编辑链接https://web.archive.org/web/20160729163958/http://eggsylife.co.uk/2009/11/30/spring-forms-dynamic-lists-and-ajax/

您可以使用以下内容

 public class InvoiceController extends SimpleFormController { protected void initBinder(HttpServletRequest request, ServletRequetDataBinder binder) throws Exception { binder.registerCustomEditor(List.class, "lineItems", new CustomCollectionEditor(List.class)() { protected Object convertElement(Object lineItem) { LineItem li = (LineItem) lineItem; // StringUtils is a jakarta Commons lang static class return (StringUtils.isBlank(li.getProductName())) ? null : li; } }); } } 

然后在onBind方法中,根据以下内容删除空引用:

 protected void onBind(HttpServletRequest request, Object command, BindException bindException) throws Exception { Invoice invoice = (Invoice) command; invoice.getLineItems().removeAll(Collections.singletonList(null)); } 

问候,

我发现在使用JSP添加/设置项目时,还需要使用GrowthList进行装饰以避免一些错误。 (还创建了一个自定义的SpringList impl。它基本上是双重装饰。)

 lineItems = GrowthList.decorate(ListUtils.lazyList(new ArrayList(), FactoryUtils.instantiateFactory(LineItem.class))); 

我同意。 问题当然是删除项目。

你可以做的是使用html中的spring标记语法。 因此,如果您使用javascript从列表中删除项目(例如索引2),那么您将使用以下标记来标记该索引:

  

然后,当提交表单时,spring将看到标记并为lineItems 2放入一个空项(基于lazylist工厂),而不是忽略它。

我今天一直在努力解决这个问题,并找出了这里描述的一些解决方案。