Question

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.

Was it helpful?

Solution

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,

OTHER TIPS

Use List instead of an array on the server side and struts will automatically map the new sections(new block of element on UI) to it.

If you define it as an array then it will not be able to initialize new section each time as the size cannot increase dynamically

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top