struts2接收参数

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

}

 访问均可以得到相应输入的结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值