struts2中 Action获取表单参数的几种方式

转自:http://blog.csdn.net/csz_363874279qqcom/article/details/5869624

struts2 Action获取表单传值

1.通过属性驱动式
JSP:
<form action="sys/login.action" method="post">
<input type="text" name="username">
<input type="submit" value="submit">
</form>

Action:直接通过get、set方法获取。

public class sysAction extends ActionSupport{
private String username;

public String login() throws Exception {
System.out.println(username);
return SUCCESS;
}

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username= username;
}
}

2.实现ServletRequestAware接口

public void setServletRequest(HttpServletRequest request) {

String name=request.getParameter("name");
this.name=name;
}

3.通过ServletActionContext

String name=ServletActionContext.getRequest().getParameter("name");

this.name=name;
4.模型驱动方式,必须要实现ModelDriven<T>接口。对于要传入多个model第二种方式不方便
JSP:
<form action="sys/login.action" method="post">
<input type="text" name="username">
<input type="submit" value="submit">
</form>

Action:必须实现getModel() 方法

public class sysAction extends ActionSupport implements ModelDriven<User>{
private User user;

public String login() throws Exception {
System.out.println(getModel().getUsername());
return SUCCESS;
}

public User getModel() {
if (null == user) {
return user = new User();
}
return user;
}
}

5.第三种方式可以完全不实现ModelDriven<T>,也可使用多个model对象的属性。
JSP:
<form action="sys/login.action" method="post">
<input type="text" name="user.username">
<input type="text" name="teacher.level">
<input type="submit" value="submit">
</form>

Action: 必须提供set方法

public class sysAction extends ActionSupport{
private User user;
private Teacher teacher;

public String login() throws Exception {
System.out.println(user.getUsername());
System.out.println(teacher.getLevel());
return SUCCESS;
}

public void setUser(User user) {
this.user = user;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}

4.模型驱动方式,必须要实现ModelDriven<T>接口。对于要传入多个model第二种方式不方便
JSP:
<form action="sys/login.action" method="post">
<input type="text" name="username">
<input type="submit" value="submit">
</form>

Action:必须实现getModel() 方法

public class sysAction extends ActionSupport implements ModelDriven<User>{
private User user;

public String login() throws Exception {
System.out.println(getModel().getUsername());
return SUCCESS;
}

public User getModel() {
if (null == user) {
return user = new User();
}
return user;
}
}

5.第三种方式可以完全不实现ModelDriven<T>,也可使用多个model对象的属性。
JSP:
<form action="sys/login.action" method="post">
<input type="text" name="user.username">
<input type="text" name="teacher.level">
<input type="submit" value="submit">
</form>

Action: 必须提供set方法

public class sysAction extends ActionSupport{
private User user;
private Teacher teacher;

public String login() throws Exception {
System.out.println(user.getUsername());
System.out.println(teacher.getLevel());
return SUCCESS;
}

public void setUser(User user) {
this.user = user;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值