Spring mvc,如何绑定具有集合作为其属性的域对象
我有一个名为 Order 的域对象,它有一个名为 serviceOrders 的集合属性,其中保存了服务集合 --- 订单 m:m 关联关系。
public class Order implements Serializable {
private Long id = null;
private BigDecimal amountPaid;
private BigDecimal accountReceivable;
private User user;
private Set serviceOrders = new HashSet();
private Date closed;
private Date created = new Date();
private String status;
还有一种用于添加名为 addServiceOrder 的关联的方法,
public void addServiceOrder(ServiceOrder serviceOrder) {
if (serviceOrder == null)
throw new IllegalArgumentException("Can't add a null serviceOrder.");
this.getServiceOrders().add(serviceOrder);
}
我应该如何使用 commandName 用“path”设置此集合,我认为它只会调用命令对象的 get set 方法。我应该如何将 serviceOrder 添加到此命令对象。我不知道这个问题。任何帮助将不胜感激
I have a domain object called Order, and it has a collection attribute called serviceOrders where a collection of service --- order m:m association relationships are hold.
public class Order implements Serializable {
private Long id = null;
private BigDecimal amountPaid;
private BigDecimal accountReceivable;
private User user;
private Set serviceOrders = new HashSet();
private Date closed;
private Date created = new Date();
private String status;
also there is a method for adding the association called addServiceOrder
public void addServiceOrder(ServiceOrder serviceOrder) {
if (serviceOrder == null)
throw new IllegalArgumentException("Can't add a null serviceOrder.");
this.getServiceOrders().add(serviceOrder);
}
how should I use commandName to set this collection with "path", I think it would only call its get set method of the Command Object. how should I add serviceOrder to this command Object. I have no idea about this problem. any help would be highly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的 ServiceOrder 实例具有唯一的 id,您的服务方法应该是 #add(Long id)。
Assuming your ServiceOrder instances have unique ids your service method should be #add(Long id).
好吧,请耐心听我说,但解决方案很简单,同时也很烦人。几个月前我遇到了这个问题。我将向您展示我的解决方案,在我看来,使用 jstl 库来处理集合。
关键位在这一行中
要链接命令对象及其集合以进行回发,您必须将元素的索引显示为弹簧路径的一部分。就我而言,我有两个级别的集合来跟踪
varStatus 使您可以访问带有索引属性的 bean 对象,您可以利用它来发挥自己的优势。
在您的情况下,您可以像我一样使用 jsp 中 foreach jstl 函数的索引属性来生成索引,并将其附加到命令对象的数组索引表示法中。命令对象当然必须遵循与路径集合名称相同的流程。这适用于无限数量的关卡,但随着关卡的进行,会变得越来越烦人。
这是一个大型的现场示例,因此如果您需要较小的东西,请向我展示您的标记,我将引导您完成它。
Ok bear with me on this one but the solution is simple an annoying at the same time. I ran into this a couple of months ago. I am going to show you my solution using the jstl libraries in my view for handling the collections.
The Key bit is in this line
To link up the command object with its collection for the postback you have to show the indice of the element as part of the spring path. In my case I have two levels of collections to track
varStatus gives you access to a bean object with the index property you can use to your advantage.
In your case you can do just use the index property of the foreach jstl function in the jsp to generate the indice like I did and append it to the array index notation of your command object. The command object must of course follow the same flow as the path collection names. This works for an infinite number of levels but gets more annoying as we go.
This is a large live example so if you need something smaller show me your markup and I will walk you throgh it.