使用 struts 1.X 的表单中可变数量的字段
我需要构建一个表单来加载一个表,该表的每一行都包含一个复选框和一个输入文本(行数是可变的,因为它是从数据库加载的)。所以我的问题是:
- 关联 formbean 应该有哪些字段?数组列表?一个 HashMap ?
- 我如何知道(提交表单后)选择了哪个复选框,以便我考虑相应的输入文本?
我使用 struts 1.X 作为框架。
提前致谢!
I need to build a form that loads a table that in each row contains a checkbox and an input text (the number of rows is variable, because it's loaded from a db). So my questions are:
- What fields should the associate formbean have ? ArrayLists ? One HashMap ?
- How can I know (once the form is submitted) what checkbox was selected so I consider the corresponding input text ?
I'm using struts 1.X as framework.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我会使用数组(列表)作为复选框,使用映射作为输入文本。您必须考虑这样一个事实:如果未选中复选框,则不会根据请求发送复选框,但始终会发送所有输入文本。因此,将复选框的值与输入文本的地图参数进行匹配,如下所示:
这意味着您将始终拥有一个包含所有值的地图:
并且还有一个选定复选框的列表,其中可以是:
然后保留文本框值仅从映射中查找在复选框值列表中找到的键。
PS 还要记住重置您的复选框。
Personally, I would use an array (list) for the checkboxes and a map for the input texts. You have to consider the fact that checkboxes are not sent on the request if they are not selected, but all your input texts are always sent. So, match the value of a checkbox with the map parameter of an input text, something like:
This means you will always have a map with all values:
and also have a list of selected checkboxes which can be:
You then keep the textbox values from the map only for the keys that are found in your list of checkbox values.
P.S. Remember also to reset your checkboxes.