JSP代码:
1 <% @ page language = " java " import = " java.util.* " pageEncoding = " utf-8 " %>
2 <% @ taglib prefix = " s " uri = " /struts-tags " %>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
6 %>
7
8 <! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
9 < html >
10 < head >
11 < base href = " <%=basePath%> " >
12
13 < title > My JSP ' index.jsp ' starting page </ title >
14 < script type = " text/javascript " src = " jquery-1.6.2.js " ></ script >
15 < script type = " text/javascript " >
16 var count = 0 ;
17 function addItems() {
18 count ++ ;
19 var str = ' <tr id= ' + count + ' ><td>用户名:<input type="text" name="users[ ' + count + ' ].username"/></td><td>密码:<input type="password" name="users[ ' + count + ' ].password"/></td><td><input type="button" value="删除本行" οnclick="removeItems( ' + count + ' );"/></td></tr> ' ;
20 $( " #div1 " ).append(str);
21 };
22 function removeItems(id) {
23 $( ' # ' + id).remove();
24 };
25
26 </ script >
27 </ head >
28
29 < body >
30 < form name = " form " action = " user/user.action " method = " post " >
31 < table >
32 < tr id = " 0 " >
33 < td > 用户名: < input type = " text " name = " users[0].username " /></ td >
34 < td > 密码: < input type = " password " name = " users[0].password " /></ td >
35 </ tr >
36 < div id = " div1 " ></ div >
37 </ table >
38 < input type = " button " value = " 添加一行 " onclick = " addItems(); " />
39 < input type = " button " value = " 添加 " onclick = " javascript:document.form.submit(); " />
40 </ form >
41
42
43 </ body >
44 </ html >
45
1
package
phiepi.action;1 <% @ page language = " java " import = " java.util.* " pageEncoding = " utf-8 " %>
2 <% @ taglib prefix = " s " uri = " /struts-tags " %>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
6 %>
7
8 <! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
9 < html >
10 < head >
11 < base href = " <%=basePath%> " >
12
13 < title > My JSP ' index.jsp ' starting page </ title >
14 < script type = " text/javascript " src = " jquery-1.6.2.js " ></ script >
15 < script type = " text/javascript " >
16 var count = 0 ;
17 function addItems() {
18 count ++ ;
19 var str = ' <tr id= ' + count + ' ><td>用户名:<input type="text" name="users[ ' + count + ' ].username"/></td><td>密码:<input type="password" name="users[ ' + count + ' ].password"/></td><td><input type="button" value="删除本行" οnclick="removeItems( ' + count + ' );"/></td></tr> ' ;
20 $( " #div1 " ).append(str);
21 };
22 function removeItems(id) {
23 $( ' # ' + id).remove();
24 };
25
26 </ script >
27 </ head >
28
29 < body >
30 < form name = " form " action = " user/user.action " method = " post " >
31 < table >
32 < tr id = " 0 " >
33 < td > 用户名: < input type = " text " name = " users[0].username " /></ td >
34 < td > 密码: < input type = " password " name = " users[0].password " /></ td >
35 </ tr >
36 < div id = " div1 " ></ div >
37 </ table >
38 < input type = " button " value = " 添加一行 " onclick = " addItems(); " />
39 < input type = " button " value = " 添加 " onclick = " javascript:document.form.submit(); " />
40 </ form >
41
42
43 </ body >
44 </ html >
45
Action代码:
2
3 import java.util.Iterator;
4 import java.util.List;
5
6 import phiepi.model.User;
7
8 import com.opensymphony.xwork2.ActionSupport;
9
10 public class UserAction extends ActionSupport {
11
12 private List < User > users;
13
14 @Override
15 public String execute() throws Exception {
16
17 for (Iterator < User > iterator = users.iterator(); iterator.hasNext();) {
18 User user = iterator.next();
19 System.out.println( " ---------- " + user.getUsername());
20 System.out.println( " ----------- " + user.getPassword());
21 }
22
23 return SUCCESS;
24 }
25
26 public List < User > getUsers() {
27 return users;
28 }
29
30 public void setUsers(List < User > users) {
31 this .users = users;
32 }
33
34 }
35