to retain values in form using struts tags and dynamic behaviour through Javascript

710 Views Asked by At

I have a JSP page containing a ADD button to add the rows(a HTML code) via Javascript. I now need to retain the value in my form by replacing the codes in JSP by struts-tags. How should I then communicate from struts-tags and JS. Since all the HTML code lies in JS, how should it use struts-tags???

Please help!!

1

There are 1 best solutions below

0
On

Your question is too vague to give an appropriate answer. However, I recently did something similar to this so I will try and give you a few guidelines.

1.) If you are hoping to populate these rows with information from the server this will require an ajax call. Most likely to an action that returns a snippet of jsp containing only a table row.

I suggest avoiding the struts2-jquery plugin for this unless you already are using it in your application. I would just use jQuery - http://api.jquery.com/jQuery.ajax/

2.) If you wish to gather user input in these rows you will just have to make sure to use appropriate naming for your fields.

Eg: In your java action you have a List< String > names. You would need to generate the following html via js.

<tbody>
   <tr>
       <input type="text" name="names[0]">
   </tr><tr>
       <input type="text" name="names[1]">
   </tr><tr>
       <input type="text" name="names[2]">
   </tr>
</tbody>

3.) If you wish to keep track of the number of added rows you could use this in your jsp...

<s:hidden name="rowsCount" id="rowsCount" value="0">

then within your javascript change the value of that input.

Hope one of those 3 helped!