struts2接收参数有三种方式:
1. Action属性(属性驱动)
2. Domain Model(对象驱动)
3. ModelDriven
方式一:属性驱动
把表单的name值作为action类的属性,并提供set方法,struts2自动接收。便可以获取参数。
1.1 jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/testParamModel">
<label>姓名:</label><input name="employeeName"><br>
<label>密码:</label><input name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
1.2 action类的编写:
package com.dsx.day1.test;
import com.opensymphony.xwork2.ActionSupport;
public class ParamAction extends ActionSupport {
private String employeeName;
private String password;
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public void setPassword(String password) {
this.password = password;
}
public String getParam() {
System.out.println("employeeName: "+employeeName);
System.out.println("password: "+password);
return SUCCESS;
}
}
1.3 struts.xml中正常配置(此处省略)
方式二:对象驱动
定义一个bean类,在action中定义一个对象并生成get,set方法。在请求的地方,请求参数有规定,必须按照规定的对象名.属性名进行传值。
2.1 jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/testParamModel">
<label>姓名:</label><input name="employee.name"><br>
<label>密码:</label><input name="employee.password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
2.2 domain类
package com.dsx.day1.test;
public class Employee {
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;
}
}
2.3 action类的编写
package com.dsx.day1.test;
import com.opensymphony.xwork2.ActionSupport;
public class ParamAction extends ActionSupport {
private Employee employee;
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Employee getEmployee() {
return employee;
}
public String getParam() {
System.out.println("employeeName: "+employee.getName());
System.out.println("password: "+employee.getPassword());
return SUCCESS;
}
}
方式三:ModelDriven模型驱动
注意:实现接口ModelDriven<泛型>,创建一个实体.重写方法。
3.1 domain实体类
package com.dsx.day1.test;
public class Employee {
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;
}
@Override
public String toString() {
return "Employee [name=" + name + ", password=" + password + "]";
}
}
3.2 jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/testParamModel">
<label>姓名:</label><input name="name"><br>
<label>密码:</label><input name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
3.2 action的编写
public class ParamAction extends ActionSupport implements ModelDriven<Employee> {
private Employee e = new Employee();
public String getParam() {
System.out.println("employee: "+e);
return SUCCESS;
}
@Override
public Employee getModel() {
// TODO Auto-generated method stub
return e;
}
}
访问均可以得到相应输入的结果。