1. Action中的属性与表单中的属性一致就可以
JSP中的表单
<form action="login.action" method="post">
用户名:<input type="text" name="username"/> <br/>
密码: <input type="password" name="password" /> <br/>
<input type="submit" value="登陆" />
</form>
Action中的属性
public class LoginAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
if( username.equalsIgnoreCase("aaa")&&password.equals("aaaaaa")){
return SUCCESS;
}
else{
return ERROR;
}
}
}
2. 使用一个VO类
在表单中提交的属性名改为user.username
<form action="login.action" method="post">
用户名:<input type="text" name="user.username"/> <br/>
密码: <input type="password" name="user.password" /> <br/>
<input type="submit" value="登陆" />
</form>
LoginAction中的属性改为user
public class LoginAction extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
if( user.getUsername().equalsIgnoreCase("aaa")&&user.getPassword().equals("aaaaaa")){
return SUCCESS;
}
else{
return ERROR;
}
}
}
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;
}
}