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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/890250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:43:31  来源:igfitidea点击:

better way for dynamic forms with Spring?

javaspringspring-mvc

提问by NA.

What I wonder is if there's a easier/better way to handle dynamic forms (adding form items to the dom via js) when using SpringMVC and Spring forms?

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

Imaging having an Invoice object that have many LineItems.

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

public class Invocie {
    private List LineItems;

    public Invoice() {
        lineItems = ListUtils.lazyList(new ArrayList<LineItem>(), FactoryUtils.instantiateFactory(LineItem.class));
    }
}

To show the items belonging to an Invoice I currently use

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

<forEach items="${invoice.lineItems}" varStatus="i">
  <form:input path="lineItems[${i.index}].productName" />
</c:forEach>

To add LineItems I have some js that calculates the new index and adds that to the DOM. When deleting a LineItem i currently have to renumber all the indexes and that's the part I'd like to avoid, is it possible?

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

回答by Arthur Ronald

You could use the following

你可以使用以下

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;
            }

        });
    }

}

Then in onBind method, you remove null references according to:

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

protected void onBind(HttpServletRequest request, Object command, BindException bindException) throws Exception {
    Invoice invoice = (Invoice) command;

    invoice.getLineItems().removeAll(Collections.singletonList(null));
}    

Regards,

问候,

回答by case nelson

I've found that also decorating with a GrowthList is necessary to avoid some errors when adding/setting items in JSP. (Also created a custom SpringList impl. that basically does the double decoration.)

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

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

I agree. The problem is certainly removing items.

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

What you can do is use the spring markersyntax in the html. So if you remove an item (at index 2 for example) from the list using javascript, you would then mark that index with:

您可以做的是在 html 中使用spring 标记语法。因此,如果您使用 javascript 从列表中删除一个项目(例如在索引 2 处),您将使用以下内容标记该索引:

<input type="hidden" name="_lineItems[2]">

Then when the form is submitted spring will see the marker and put in an empty item (based on the lazylist factory) for lineItems2instead of ignoring it.

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

回答by eggsy84

I've implemented a tutorial that might help you solve this using jQuery on the client side and Springs AutoPopulating list for you form backing objects.

我已经实现了一个教程,可以帮助您在客户端使用 jQuery 和 Springs AutoPopulating 列表来帮助您解决这个问题,以形成支持对象。

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

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

EDIT Link from Webarchive https://web.archive.org/web/20160729163958/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/

回答by Lukasz Frankowski

I've been struggling with this problem today and figured out some solution described here.

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