简体   繁体   中英

struts2 and Jquery

I'm adding rows in a table dynamically in a JSP Table using Jquery and want to send that table data in Struts2 action List ( for eg )

Below is Code for Adding Rows dynamically, like in below Link :

how-to-add-remove-table-rows-dynamically-using-jquery

And i want to send this Table Data to Struts 2 Action in a List of Employee Object lets say :

public class MyAction extends BaseAction {

   private List<Emp> emplList ;

   // Getters + Setters of emplList  

I tried to use s:iterator but its not working, emplList.size is 0 ( zero ) in Action

can someone please suggest me or give some sample Code

Given that Emp has name and mail properties with setters and getters, there are probably 2 ways of doing this:

  1. You modify your jQuery javascript so that the end result of added rows will become something like:
<input type="text" name="emplList[0].name"   value="Name1"/>  
<input type="text" name="emplList[0].email"  value="email1"/>
<input type="text" name="emplList[1].name"   value="Name2"/>
<input type="text" name="emplList[1].email"  value="Email2"/> 

and then, after submit is pressed your List of Emp in Action should be populated.

The downside of this solution is that you have to provide for correct indexing before sending the data to the server (could be tricky if you delete rows in process of building of data table)

  1. To avoid indexing hassle, in your Action you could declare 2 Lists

    private List<String> names; private List<String> emails;

    in that case, end result of your javascript would be

<input name="names" value="name1" />
<input name="emails" value="email1" />
<input name="names" value="name2" />
<input name="emails" value="email2" />

after the submit, two new lists ( names , emails ) on server side will be filled, from which you could created your desires List of Emp with something like

emplList.add(new Emp(names.get(i),emails.get(i))); for each i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM