简体   繁体   English

struts2和Jquery

[英]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 ) 我正在使用Jquery在JSP表中动态添加表中的行,并希望在Struts2操作列表中发送该表数据(例如)

Below is Code for Adding Rows dynamically, like in below Link : 下面是用于动态添加行的代码,如下面的Link所示:

how-to-add-remove-table-rows-dynamically-using-jquery 如何对附加删除表-行-动态使用,jquery的

And i want to send this Table Data to Struts 2 Action in a List of Employee Object lets say : 我想将此表数据发送到Employee对象列表中的Struts 2 Action:

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 我尝试使用s:iterator但它不起作用,在Action emplList.size为0(零)

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: 鉴于Emp具有使用setter和getter的namemail属性,大概有两种方法可以这样做:

  1. You modify your jQuery javascript so that the end result of added rows will become something like: 您修改jQuery javascript,以便添加行的最终结果将变为:
<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. 然后,按下后提交ListEmpAction应该被填充。

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 为了避免索引麻烦,您可以在Action中声明2个列表

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

    in that case, end result of your javascript would be 在这种情况下,您的javascript的最终结果将是

<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 提交后,将在服务器端填充两个新列表( namesemails ),您可以根据这些List创建所需的Emp List

emplList.add(new Emp(names.get(i),emails.get(i))); for each i 对于每个i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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