Struts2 - 编辑复合对象

发布于 2024-10-15 05:11:09 字数 427 浏览 1 评论 0原文

我有一个复杂的对象,它由许多其他类型的对象组成。作为示例,我们假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2024-10-22 05:11:09

如果设置器目标是列表或数组,您可以发送多个具有相同名称的参数,它们将被添加到列表中。如果有意义的话,您还可以使用数组表示法在特定位置插入。

例如,这应该可行:

user[x].address[y].houseNumber

如果目标是地图,则 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文