struts2中Action怎样获取动态表单中的多行数据?
我的页面如下,每点击一次“添加”按钮就生成一行表格,当所有信息输入完毕后点击“提交”按钮将值传给Action。如果行数已经确定的话可以在Action中通过get()方法来获取,但现在行数不确定,请教大家如何在Action中获取到这多行数据?
我的HTML代码如下:
- HTML code
<html><head><script type="text/javascript"> $().ready(function(){ //添加学生 $("#bt_add").click(function(){ var tr = $("#datalist table tbody:last-child"); //alert($("#datalist table tbody:last-child").html()); $("#datalist table").append($(".trmodel").clone().show().removeClass()); }); $(".delete").live('click',function(){ $(this).parents("tr").animate({ opacity: 'hide', left:'+250px', top:'-120px' }, 1000); }); });</script></head><body> <div id="title"> <h2>添加学生</h2> <div> 入学年份 <select style="width:80px;"><option>2008</option></select> 系别 <select style="width:150px;"><option>计算机与科学系</option></select> 班级 <select style="width:150px;"><option>90812P</option></select> <span class="buttons"> <input type="button" value="提交" /> <input type="button" value="添加" id="bt_add" /> </span> </div> </div> <div style="clear:both;"></div> <!--表格样式--> <div id="datalist"> <table> <thead> <td width="20%">学号</td> <td width="20%">学生姓名</td> <td width="10%">性别</td> <td width="20%"></td> </thead> <tbody> <tr style=""> <td align="left"><input type="text" value="" /></td> <td align="left"><input type="text" value="" /></td> <td> <label> <input type="radio" name="RadioGroup1" value="男" id="RadioGroup1_0" checked="checked" class="input_align_5"/> 男</label> <label> <input type="radio" name="RadioGroup1" value="女" id="RadioGroup1_1" class="input_align_5" /> 女</label> </td> <td></td> </tr> <tr style=" display:none;" class="trmodel"> <td align="left"><input type="text" value="" /></td> <td align="left"><input type="text" value="" /></td> <td> <label> <input type="radio" name="RadioGroup1" value="男" id="RadioGroup1_0" checked="checked" class="input_align_5"/> 男</label> <label> <input type="radio" name="RadioGroup1" value="女" id="RadioGroup1_1" class="input_align_5" /> 女</label> </td> <td><a href="#" class="delete"> 删除</a></td> </tr> </tbody> </table> </div></body></html>
页面如下:
[解决办法]
String[] number= request.getParameter("学号input的name");
String[] name = request.getParameter("姓名input的name");
。。。。
[解决办法]
你的表的每一行都属于一个对象,那么一张表就可以放到集合里面;
在struts2中 可以使用类型转换来做
XXXXAction类中 可以定义一个list;(前提是你有一个student的entity类,也就是实体类)
例如:List<student> stus;
让这个量生成get,set方法;
在你的jsp页面中 表格中的td里面你可以存放struts2自带的标签,或者html标签都可以;
请求如:<input type="text" name="stus.id" >
:<input type="text" name="stus.name" >
:<input type="text" name="stus.sex" >
这样提交后 sturts2 自动做类型转换,表格中的每一行都代表student的一个对象,它会自动帮你创建student对象并且赋值,你可以在XXXXAction类中打印这个list集合看看是否是你要的数据