Struts2 submit a form using request and respone most common way with a simple array, as well as an array of complex Map assignment.

A, ServletActionContext () Gets request and respone

The most common way, other Servlet and work just like the code below:

 

1         HttpServletRequest request = ServletActionContext.getRequest();
2         HttpServletResponse response = ServletActionContext.getResponse();

 

Note: The above method need to write in the Action class.

Second, the use of ordinary arrays, complex arrays, Map assignment

(1) using ordinary array, Action code classes and reception are as follows:

 

 1 private String[] list;
 2 
 3 public void setList(String[] list) {
 4      this.list = list;
 5 }
 6 
 7 public String[] getList() {
 8      return list;
 9 }
10 
11 @Override
12     public String execute() throws Exception {
13         ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
14         ServletActionContext.getResponse().getWriter().println(Arrays.asList(list));
15         return NONE;
16     }

 

 

1            <form action="${pageContext.request.contextPath}/userAdd" method="get">
2                <input type="text" name="list" value="张三">
3                <input type="text" name="list" value="李四">
4                <button>添加</button>
5            </form>

Result [John Doe]

(2) using an array complicated, and this time we pass a good package to List User, code is as follows :

 1 public class User {
 2     private String userName;
 3     private String userPwd;
 4 
 5     public void setUserName(String userName) {
 6         this.userName = userName;
 7     }
 8 
 9     public void setUserPwd(String userPwd) {
10         this.userPwd = userPwd;
11     }
12 
13     public String getUserName() {
14         return userName;
15     }
16 
17     public String getUserPwd() {
18         return userPwd;
19     }
20     public String toString(){
21         return "[userName:"+getUserName()+",userPwd:"+getUserPwd()+"]";
22     }
23 }

Then create a class in the Action List, modify the code reception

 1     private List<User> user;
 2 
 3     public List<User> getUser() {
 4         return user;
 5     }
 6 
 7     public void setUser(List<User> user) {
 8         this.user = user;
 9     }
10 
11     @Override
12     public String execute() throws Exception {
13         ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
14         ServletActionContext.getResponse().getWriter().println(Arrays.asList(user));
15         return NONE;
16     }
1            <form action="${pageContext.request.contextPath}/userAdd" method="get">
2                <input type="text" name="user[0].userName" value="张三">
3                <input type="text" name="user[0].userPwd" value="123三">
4                <input type="text" name="user[1].userName" value="李四">
5                <input type="text" name="user[1].userPwd" value="123四">
6                <button>添加</button>
7            </form>

Result [[[userName: Zhang, userPwd: 123 three], [userName: John Doe, userPwd: 123 four]]]

(4) using the User Map added to it, and add the key, as follows

 

 1     private Map<String,User> map;
 2 
 3     public Map<String, User> getMap() {
 4         return map;
 5     }
 6 
 7     public void setMap(Map<String, User> map) {
 8         this.map = map;
 9     }
10     @Override
11     public String execute() throws Exception {14         ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
15         ServletActionContext.getResponse().getWriter().println(Arrays.asList(map));
16         return NONE;
17     }

 

1            <form action="${pageContext.request.contextPath}/userAdd" method="get">
2                <input type="text" name="map['a'].userName" value="张三">
3                <input type="text" name="map['a'].userPwd" value="123三">
4                <input type="text" name="map['b'].userName" value="李四">
5                <input type="text" name="map['b'].userPwd" value="123四">
6                <button>添加</button>
7            </form>

Result [{[userName a =: Zhang, userPwd: 123 three], b = [userName: John Doe, userPwd: 123 four]}]

To sum up, in Struts2, the assignment of the array in Action inside wording are similar, important to note that the front desk of code written in a different way. (Struts2 study notes)

  --2019.7.4 me in the winter, betting that next spring

 

Guess you like

Origin www.cnblogs.com/zemengcheng/p/11135050.html