Struts2 - 编辑复合对象 [英] Struts2 - Edit Composite Object
问题描述
我有一个由许多其他类型的对象组成的复杂对象。作为一个例子,我们假设我有一个用户类型,并且每个用户对象包含许多地址实例。地址包含房屋号码(int)和街道名称(字符串)。我想制作一个表单来编辑该用户对象及其所有地址。我怎么做?我知道如果用户有一个地址,我会怎么做。它看起来像下面这样(假设正确的getter和setter):
I have a complex object that is composed of many of another type of object. As an example let's assume that I have a User type, and each User object contains many Address instances. Address contains a house number (int) and street name (String). I would like to make a single form to edit that user object and all of its addresses. How do I do that? I know how I would do it if User had a single Address. It would look something like the following (assuming the proper getters and setters):
//In the JSP
<s:textfield name="user.address.houseNumber/>
//In the Action
void setUser(User user) {...}
神奇的是,User对象会被提交新的门牌号码,但是如何编辑像我列举的对象集合以上?我必须在某处更改输入标签的名称,并在用户类型上有一些特殊名称的设置器吗?
Magically, the User object would be submitted with the new house number. But how does this work when editing collections of objects like I have enumerated above? Do I have to change the name of an input tag somewhere and have some specially-named setter on the User type?
推荐答案
如果setters target是一个列表或数组,您可以发送多个具有相同名称的参数,并将它们添加到列表中。如果有意义的话,还可以使用数组表示法来插入特定位置。
If the setters target is an a list or array you can send multiple parameters with the same name and they will be added to the list. You can also use array notation to insert at a specific location if that makes sense.
举例来说,这应该是正常的:
As an example, this should work:
user[x].address[y].houseNumber
如果目标是地图,则x和y必须是合适的钥匙。更少的是你在JSP中使用的访问嵌套组件的内容用于表单。
If the target is a map then x and y are required to be suitable keys. More less what you use in the JSP's to access nested components goes for the form.
这篇关于Struts2 - 编辑复合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!