如何在Struts 2中提交多个模型?

时间:2022-11-23 21:50:20

I have a JSP that allows users to dynamically create additional form fields to create multiple objects. Perhaps I want to allow users to be able to submit as many line items as they want when submitting an invoice form.

我有一个JSP,允许用户动态创建其他表单字段来创建多个对象。也许我想允许用户在提交发票表单时能够提交他们想要的任意数量的订单项。

How do I create a Struts 2 Action that will be able to take in an ArrayList populated with objects created from those dynamically generated fields.

如何创建一个Struts 2 Action,它将能够接收一个ArrayList,该ArrayList填充了从这些动态生成的字段创建的对象。

2 个解决方案

#1


1  

You should read the Tabular input guide.

您应该阅读表格输入指南。

#2


1  

According to the (ever-poor) documentation, which forces you to try to extrapolate the information you want, rather than just telling you authoritatively (and assuming you're really asking about Struts' built-in type conversion), your form fields would need to be named something like...

根据(一直很糟糕的)文档,它强迫你试图推断你想要的信息,而不是仅仅权威地告诉你(假设你真的在询问Struts的内置类型转换),你的表单字段会需要被命名为......

someList.makeNew(0).someField1
someList.makeNew(0).someField2
...
someList.makeNew(1).someField1
someList.makeNew(1).someField1
...

...and you would then need to set up an ActionClassName-conversion.properties file to let the type converter know how to handle type conversion for fields which begin with "someList".

...然后,您需要设置一个ActionClassName-conversion.properties文件,让类型转换器知道如何处理以“someList”开头的字段的类型转换。

The only time I actually tried this myself, I had trouble getting it working with Lists and ended up having to use Maps.

我自己实际尝试过的唯一一次,我无法使用Lists并最终不得不使用Maps。

Here's a useful blog entry about modifying a Map of objects using type conversion - I haven't had much luck finding useful information about the "makeNew" field name format the documentation mentions, but this might help you get started.

这是一篇关于使用类型转换修改对象Map的有用博客文章 - 我没有太多运气找到有关文档提到的“makeNew”字段名称格式的有用信息,但这可能有助于您入门。

#1


1  

You should read the Tabular input guide.

您应该阅读表格输入指南。

#2


1  

According to the (ever-poor) documentation, which forces you to try to extrapolate the information you want, rather than just telling you authoritatively (and assuming you're really asking about Struts' built-in type conversion), your form fields would need to be named something like...

根据(一直很糟糕的)文档,它强迫你试图推断你想要的信息,而不是仅仅权威地告诉你(假设你真的在询问Struts的内置类型转换),你的表单字段会需要被命名为......

someList.makeNew(0).someField1
someList.makeNew(0).someField2
...
someList.makeNew(1).someField1
someList.makeNew(1).someField1
...

...and you would then need to set up an ActionClassName-conversion.properties file to let the type converter know how to handle type conversion for fields which begin with "someList".

...然后,您需要设置一个ActionClassName-conversion.properties文件,让类型转换器知道如何处理以“someList”开头的字段的类型转换。

The only time I actually tried this myself, I had trouble getting it working with Lists and ended up having to use Maps.

我自己实际尝试过的唯一一次,我无法使用Lists并最终不得不使用Maps。

Here's a useful blog entry about modifying a Map of objects using type conversion - I haven't had much luck finding useful information about the "makeNew" field name format the documentation mentions, but this might help you get started.

这是一篇关于使用类型转换修改对象Map的有用博客文章 - 我没有太多运气找到有关文档提到的“makeNew”字段名称格式的有用信息,但这可能有助于您入门。