views:

965

answers:

1

Hello friends,

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>

`

Now each time user clicks on button whole block given above will be included in html form using ajax method call.

  1. Now my problem is how can I get values of all elements as an array in struts2 action class. Or How to write setter and getter method for these arrays.

Is there any better way to perform above in other way which make work easier for me?

  1. How to write struts2 validation file for array fields like this?

Can anyone help me in this matter.

Thanks.

+1  A: 

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.

Regards,

pedromarce