Struts2中表单与Action传递数据三种方式

来源:互联网 发布:insanity知乎 编辑:程序博客网 时间:2024/05/12 17:46

1、Action中的属性与表单中的属性一致就可以(字段的属性对应页面表单字段的属性相同)

JSP中的表单

<formaction="login.action"method="post">用户名:<inputtype="text"name="username"/> < br/>密码: <inputtype="password"name="password"/>< br/><inputtype="submit"value="登陆"/></form>

Action中的属性

public class LoginAction extends ActionSupport {private String username;private String password;public String getUsername() {    returnusername;    }publicvoid setUsername(String username) {    this.username = username;    }public String getPassword() {    returnpassword;    }publicvoid setPassword(String password) {    this.password = password;    }public String execute(){if( username.equalsIgnoreCase("aaa")&&password.equals("aaaaaa")){    returnSUCCESS;}    else{        returnERROR;        }    }}

2、 使用一个VO类

在表单中提交的属性名改为user.username

<formaction="login.action"method="post">用户名:<inputtype="text"name="user.username"/> <br/>密码: <inputtype="password"name="user.password"/><br/><inputtype="submit"value="登陆"/></form>

LoginAction中的属性改为user

publicclassLoginActionextends ActionSupport{private User user;public User getUser() {returnuser;}publicvoid setUser(User user) {this.user = user;}public String execute(){if( user.getUsername().equalsIgnoreCase("aaa")&&user.getPassword().equals("aaaaaa")){returnSUCCESS;}    else{            returnERROR;        }    }}

3、 使用Struts2中的ModelDriven数据模式

Action类要实现一个泛型接口,前台表单与1相同

public class LoginAction extends ActionSupport implements ModelDriven<User> {private User user = new User();public String execute(){if( user.getUsername().equalsIgnoreCase("aaa")&&user.getPassword().equals("aaaaaa")){return SUCCESS;}else{return ERROR;}}public User getModel() {            return user;                }    }
0 0
原创粉丝点击