Struts2 Study Notes (6) -- Action Processing request parameters, struts2 Study Notes
In the struts2 framework, there are two methods (three methods) for processing Request Parameters by Action ), the form attribute name should be the same as the member attribute defined in the Action class or the member attribute name defined in the JavaBean:
1. Property-driven
1) define the member attributes in the Action class to receive request parameters (Use Action as a javaBean). In the Action, you also need to define the setter method of the member attributes.
Form Information:
1 <form action = "$ {pageContext. servletContext. contextPath}/testAction. action "> 2 name: <input type =" text "name =" name "> <br> 3 password: <input type = "password" name = "password"> <br> 4 <input type = "submit" value = "submit"> 5 </form>
Action class:
1 public class TestAction { 2 private String name; 3 private String password; 4 5 public String getName() { 6 return name; 7 } 8 9 public void setName(String name) {10 this.name = name;11 }12 13 public String getPassword() {14 return password;15 }16 17 public void setPassword(String password) {18 this.password = password;19 }20 21 public String execute() {22 System.out.println("name: " + name);23 System.out.println("password: " + password);24 return "success";25 }26 }
Front-end input information:
Result displayed in the background:
2) You can directly use javaBean as the Action attribute. On the page, you must use an ognl expression to describe the component's name attribute.
1 username:<input type="text" name="user.username"><br>
JavaBean:
1 public class Person { 2 private String name; 3 private String password; 4 5 public String getName() { 6 return name; 7 } 8 public void setName(String name) { 9 this.name = name;10 }11 public String getPassword() {12 return password;13 }14 public void setPassword(String password) {15 this.password = password;16 }17 }
Form Information:
1 <form action = "$ {pageContext. servletContext. contextPath}/testAction. action "> 2 name: <input type =" text "name =" person. name "> <br> 3 password: <input type =" password "name =" person. password "> <br> 4 <input type =" submit "value =" submit "> 5 </form>
Action:
1 public class TestAction { 2 private Person person; 3 4 public Person getPerson() { 5 return person; 6 } 7 8 public void setPerson(Person person) { 9 this.person = person;10 }11 12 public String execute() {13 System.out.println("name: " + person.getName());14 System.out.println("password: " + person.getPassword());15 return "success";16 }17 }
Front-end input information:
Result displayed in the background:
This method uses the name = "params" Interceptor to encapsulate parameters.
1 <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
2. model-driven
The process for model-driven request parameters is as follows:
Steps:
- 1. Action must implement the ModelDriven Interface
- 2. Override the getModel () method and return a javaBean object.
- 3. instantiate a javaBean object.
JavaBean:
1 public class Person { 2 private String name; 3 private String password; 4 public String getName() { 5 return name; 6 } 7 public void setName(String name) { 8 this.name = name; 9 }10 public String getPassword() {11 return password;12 }13 public void setPassword(String password) {14 this.password = password;15 }16 }
Form Information:
1 <form action = "$ {pageContext. servletContext. contextPath}/testAction. action "> 2 name: <input type =" text "name =" name "> <br> 3 password: <input type = "password" name = "password"> <br> 4 <input type = "submit" value = "submit"> 5 </form>
Action:
1 public class TestAction implements ModelDriven { 2 private Person person = new Person(); 3 4 public Person getPerson() { 5 return person; 6 } 7 8 public void setPerson(Person person) { 9 this.person = person;10 }11 12 public String execute() {13 System.out.println("name: " + person.getName());14 System.out.println("password: " + person.getPassword());15 return "success";16 }17 18 @Override19 public Object getModel() {20 return person;21 }22 }
Front-end input information:
Result displayed in the background:
4. encapsulate data to Collection or Array
JavaBean:
1 public class Person { 2 private String name; 3 private String password; 4 public String getName() { 5 return name; 6 } 7 public void setName(String name) { 8 this.name = name; 9 }10 public String getPassword() {11 return password;12 }13 public void setPassword(String password) {14 this.password = password;15 }16 }
Form Information:
1 <form action = "$ {pageContext. servletContext. contextPath}/testAction. action "> 2 name1: <input type =" text "name =" list [0]. name "> <br> 3 password1: <input type =" password "name =" list [0]. password ">
Action:
1 public class TestAction { 2 private List<Person> list; 3 4 public List<Person> getList() { 5 return list; 6 } 7 8 public void setList(List<Person> list) { 9 this.list = list;10 }11 12 public String execute() {13 System.out.println("name1: " + list.get(0).getName());14 System.out.println("password1: " + list.get(0).getPassword());15 System.out.println("name2: " + list.get(1).getName());16 System.out.println("password2: " + list.get(1).getPassword());17 return "success";18 }19 }
Front-end input information:
Result displayed in the background:
5. encapsulate data to Map
JavaBean:
1 public class Person { 2 private String name; 3 private String password; 4 public String getName() { 5 return name; 6 } 7 public void setName(String name) { 8 this.name = name; 9 }10 public String getPassword() {11 return password;12 }13 public void setPassword(String password) {14 this.password = password;15 }16 }
Form Information:
1 <form action = "$ {pageContext. servletContext. contextPath}/testAction. action "> 2 name1: <input type =" text "name =" map ['one']. name "> <br> 3 password1: <input type =" password "name =" map ['one']. password ">
Action:
public class TestAction { private Map<String, Person> map; public Map<String, Person> getMap() { return map; } public void setMap(Map<String, Person> map) { this.map = map; } public String execute() { System.out.println("name1: " + map.get("one").getName()); System.out.println("password1: " + map.get("one").getPassword()); System.out.println("name2: " + map.get("two").getName()); System.out.println("password2: " + map.get("two").getPassword()); return "success"; }}
Front-end input information:
Result displayed in the background: