开发者

Struts2 - Edit Composite Object

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 cont开发者_运维技巧ains 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) {...}

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?


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜