Struts2学习笔记(六)——Action处理请求参数

时间:2021-11-06 15:15:58

在struts2框架中关于Action处理请求参数有两种方案(三个方式),表单属性的名称应该和在Action类中定义的成员属性或者在JavaBean中定义的成员属性名称一样:

1、属性驱动

1)直接在Action类中定义成员属性来接收请求参数 (将Action当成javaBean),在Action中还需要定义成员属性的setter方法。

表单信息:

 <form action="${pageContext.servletContext.contextPath}/testAction.action">
name:<input type="text" name="name"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>

Action类:

 public class TestAction {
private String name;
private String password; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String execute() {
System.out.println("name: " + name);
System.out.println("password: " + password);
return "success";
}
}

前台输入信息:

Struts2学习笔记(六)——Action处理请求参数

后台显示结果:

Struts2学习笔记(六)——Action处理请求参数

2)直接将javaBean做为Action的属性,在页面上必须使用ognl表达式来描述组件的name属性

 username:<input type="text" name="user.username"><br>

JavaBean:

 public class Person {
private String name;
private String password; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

表单信息:

 <form action="${pageContext.servletContext.contextPath}/testAction.action">
name:<input type="text" name="person.name"><br>
password:<input type="password" name="person.password"><br>
<input type="submit" value="提交">
</form>

Action:

 public class TestAction {
private Person person; public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} public String execute() {
System.out.println("name: " + person.getName());
System.out.println("password: " + person.getPassword());
return "success";
}
}

前台输入信息:

Struts2学习笔记(六)——Action处理请求参数

后台显示结果:

Struts2学习笔记(六)——Action处理请求参数

这种方式是通过name="params"的拦截器实现参数封装的。

 <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

2、模型驱动

模型驱动处理请求参数的步骤为:

步骤:

  • 1.Action必须实现ModelDriven接口
  • 2.重写getModel()方法,返回一个javaBean对象.
  • 3.实例化一个javaBean对象.

JavaBean:

 public class Person {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

表单信息:

 <form action="${pageContext.servletContext.contextPath}/testAction.action">
name:<input type="text" name="name"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>

Action:

 public class TestAction implements ModelDriven {
private Person person = new Person(); public Person getPerson() {
return person;
} public void setPerson(Person person) {
this.person = person;
} public String execute() {
System.out.println("name: " + person.getName());
System.out.println("password: " + person.getPassword());
return "success";
} @Override
public Object getModel() {
return person;
}
}

前台输入信息:

Struts2学习笔记(六)——Action处理请求参数

后台显示结果:

Struts2学习笔记(六)——Action处理请求参数

4、封装数据到Collection或数组

JavaBean:

 public class Person {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

表单信息:

 <form action="${pageContext.servletContext.contextPath}/testAction.action">
name1:<input type="text" name="list[0].name"><br>
password1:<input type="password" name="list[0].password"><hr>
name2:<input type="text" name="list[1].name"><br>
password2:<input type="password" name="list[1].password"><br>
<input type="submit" value="提交">
</form>

Action:

 public class TestAction {
private List<Person> list; public List<Person> getList() {
return list;
} public void setList(List<Person> list) {
this.list = list;
} public String execute() {
System.out.println("name1: " + list.get(0).getName());
System.out.println("password1: " + list.get(0).getPassword());
System.out.println("name2: " + list.get(1).getName());
System.out.println("password2: " + list.get(1).getPassword());
return "success";
}
}

前台输入信息:

Struts2学习笔记(六)——Action处理请求参数

后台显示结果:

Struts2学习笔记(六)——Action处理请求参数

5、封装数据到Map

JavaBean:

 public class Person {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

表单信息:

 <form action="${pageContext.servletContext.contextPath}/testAction.action">
name1:<input type="text" name="map['one'].name"><br>
password1:<input type="password" name="map['one'].password"><hr>
name2:<input type="text" name="map['two'].name"><br>
password2:<input type="password" name="map['two'].password"><br>
<input type="submit" value="提交">
</form>

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";
}
}

前台输入信息:

Struts2学习笔记(六)——Action处理请求参数

后台显示结果:

Struts2学习笔记(六)——Action处理请求参数