Struts2 - 编辑复合对象
我有一个复杂的对象,它由许多其他类型的对象组成。作为示例,我们假设我有一个 User 类型,并且每个 User 对象包含许多 Address 实例。地址包含门牌号 (int) 和街道名称 (String)。我想制作一个表单来编辑该用户对象及其所有地址。我该怎么做?我知道如果用户有一个地址我会怎么做。它看起来像下面这样(假设有正确的 getter 和 setter):
//In the JSP
<s:textfield name="user.address.houseNumber/>
//In the Action
void setUser(User user) {...}
神奇的是,User 对象将与新的门牌号一起提交。但是,当编辑像我上面列举的对象集合时,这是如何工作的呢?我是否必须在某处更改输入标签的名称,并在用户类型上有一些专门命名的设置器?
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) {...}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果设置器目标是列表或数组,您可以发送多个具有相同名称的参数,它们将被添加到列表中。如果有意义的话,您还可以使用数组表示法在特定位置插入。
例如,这应该可行:
如果目标是地图,则 x 和 y 需要是合适的键。更不用说您在 JSP 中用于访问嵌套组件的内容适用于表单。
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:
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.