在struts 开发中我们都知道页面表单的数据提交,首先要经过struts拦截器,然后在后台的action类中,使用set、get方法来得到表单传过来的数据值,那么这时候我们就必须在action类中定义表单的数据变量及其set、get方法,而有开发经验的人都知道,我们的开发是基于MVC模式的,页面表单的数据变量在model类中都已经定义过,且有set、get方法了,如果为了接收数据又在action重新定义了表单变量及其set、get方法,这样纯粹是增加了代码量,且action中的表单变量及其set、get方法看起来是多余的,那有没有解决办法那,action中不用重新定义表单变量及其set、get方法就能接收数据,下面就给出解决方法。
我们只要在action中定义一个model类对象和其对应的set、get方法就可以解决这个问题,如:首先定义个user model类,定义所有的变量及set、get方法,然后在action类中直接定义这个model 类user的对象及set、get方法,就可以接收页面传过来的数据了,而不用在action中把所有表单变量再重新定义一次。
package org.xmh.demo;
publicclass User {
private String name;
private String password;
....省略set、get方法
}
package org.xmh.demo;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private User user;
private Stringmessage;
public String getMessage() {
return message;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}