Struts2 System Training of Action 6 three methods of receiving parameters of action
We know that action plays a controller role in web development. By receiving parameters from the client, different modules are executed to implement the operation. Therefore, receiving parameters is an important part, only front-end parameters can be received before database operations and other background business logic can be executed. Then, let's start to display these three methods one by one (just look at the compilation of the action class)
1. Method 1: UserAction. java
package com.bjsxt.struts2.user.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private String name;private int age;public String add() {System.out.println("name=" + name);System.out.println("age=" + age);return SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}In UserAction. java defines the attributes and corresponding methods of actions to receive front-end parameters. This is the simplest and most intuitive, but the premise is that you know what the parameters passed by the front-end are, lack of flexibility, so there is a second method:
1UserAction. the java code is
package com.bjsxt.struts2.user.action;import com.bjsxt.struts2.user.model.User;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private User user;//private UserDTO userDTO;public String add() {System.out.println("name=" + user.getName());System.out.println("age=" + user.getAge());return SUCCESS;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}UserDTO. java code:
package com.bjsxt.struts2.user.dto;public class UserDTO {private String name;private String password;private String confirmingPassword;}
User. java code:
package com.bjsxt.struts2.user.model;public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}This method writes the steps for receiving parameters separately, defines attributes in UserDTO, and defines attributes in User. Two classes are introduced through import in action to receive data. This method was first introduced in my work. I felt that this method was particularly troublesome. Why should I write it separately? Later I understood it slowly, which not only reduced the coupling of the program, well planned program expansion and other operations. However, some people prefer the third method. Due to limited space, I will analyze it in detail in the next article and look forward to it...