如何访问与struts2的形式数组字段 [英] how to access form array fields with struts2
问题描述
目前我的工作,其中包含2个按钮,一个窗体上。单击1按钮我得到的HTML领域块如下图所示。
Currently I am working on one form which contains 2 buttons. Clicking on 1 button I am getting block of html fields as below.
`
<table width="100%">
<tbody><tr>
<td class="style4" width="12%" align="center">CODE<span class="style3"> </span></td>
<td width="18%"><input name="c_code[]" value="" id="c_code__" class="box_border" style="width: 120px;" type="text"></td>
<td class="style3" width="15%" align="right">Price</td>
<td width="7%"><input name="c_price[]" value="" id="c_price__" class="box_border" style="width: 40px;" type="text"></td>
<td class="style3" width="4%" align="right">Qty</td>
<td width="7%"><input name="c_qty[]" value="" id="c_qty__" class="box_border" style="width: 40px;" type="text"></td>
<td class="style3" width="9%" align="right">On Sale </td>
<td width="3%"><input name="c_onsale[]" id="c_onsale__" value="" type="radio">
<label for="c_onsale"></label>
</td>
<td class="style3" width="10%" align="center">Exposition</td>
<td width="15%"><input name="c_exposition[]" size="15" value="" id="c_exposition__" class="box_border" type="text"></td>
</tr>
</tbody></table>
`
现在上面给出的按钮全块,每次用户点击将用ajax方法调用包含在HTML表单。
Now each time user clicks on button whole block given above will be included in html form using ajax method call.
- 现在,我的问题是我如何能得到的所有元素在Struts2的action类数组的值。
或如何写这些阵列setter和getter方法。
有没有其他的方式执行上面什么更好的方法这使得工作变得更容易了吗?
Is there any better way to perform above in other way which make work easier for me?
- 如何写Struts2的校验文件这样的数组字段?
谁能帮我在这件事情。
感谢。
推荐答案
好吧,如果你可以在阵列每次迭代中使用索引重命名域,所以他们会被命名为c_name [0],c_name [1]等等......然后,你可以在你的Action类定义数组和映射将毫无问题地完成。
至于验证我会建议你采取行动的服务器端验证。
Ok, if you can rename your fields using an index in the array for each iteration, so they'll be named c_name[0], c_name[1], etc... Then you can just define an array in your action class and the mapping will be done without problems. As for validation I would suggest a server-side validation on your action.
问候,
这篇关于如何访问与struts2的形式数组字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!