Struts2的三种接收用户输入的方法
?
这种方式最简单,在action 中创建需要的属性,然后添加getter 、setter 方法:
代码清单1 : Login1Action.java
?
package com.coderdream.action;
public class Login1Action {
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 ("aaa" .equalsIgnoreCase(username .trim())
&& "123" .equalsIgnoreCase(password .trim())) {
return "success" ;
} else {
return "failure" ;
}
}
}
?
?
在jsp 中调用(输入):
代码清单 2 : login.jsp 片段
?
1 、利用 action 类的属性接收用户输入:
< br />
< form action =" <%= path%> /login1.action" method ="POST" >
姓名:
< input type ="text" name ="username " >
< br />
密码:
< input type ="text" name ="password " >
< br />
< input type ="submit" name =" 登录 " >
< br />
</ form > ?
在jsp 中调用(输出):
代码清单 3 : login1Suc.jsp 片段
欢迎 < s:property value ="username " /> ,登录成功!
?
?
先创建VO 对象,在对象中创建需要的属性,然后添加getter 、setter 方法:
代码清单 4 : User.java
?
package com.coderdream.vo;
public class User {
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;
}
}
?
?
在action 中创建User 对象,并添加该对象的getter 、setter 方法:
代码清单 5 : Login2Action.java
?
package com.coderdream.action;
import com.coderdream.vo.User;
import com.opensymphony.xwork2.Action;
public class Login2Action implements Action {
private User user ;
public User getUser() {
return user ;
}
public void setUser(User user) {
this .user = user;
}
public String execute() {
if ("aaa" .equalsIgnoreCase(user .getUsername().trim())
&& "123" .equalsIgnoreCase(user .getPassword().trim())) {
return SUCCESS ;
} else {
return INPUT ;
}
}
}
?
?
在jsp 中调用,需要使用user 对象名(输入):
代码清单 6 : login.jsp 片段
?
2 、利用领域对象( VO )接收用户输入:
< br />
< form action =" <%= path%> /login2.action" method ="POST" >
姓名:
< input type ="text" name ="user.username " >
< br />
密码:
< input type ="text" name ="user.password " >
< br />
< input type ="submit" name =" 登录 " >
< br />
</ form >
?
在jsp 中调用,需要使用user 对象名(输出):
代码清单 7 : login1Suc.jsp 片段
欢迎 < s:property value ="user.username " /> ,登录成功!?
使用这种方式时,需要实现下面的接口:
?
com.opensymphony.xwork2 Interface ModelDriven <T> public interface ModelDriven<T>ModelDriven Actions provide a model object to be pushed onto the ValueStack in addition to the Action itself, allowing a FormBean type approach like Struts.?
然后在action 中创建需要的模型,最后实现getModel() 方法:
代码清单 8 : Login3Action.java
?
package com.coderdream.action;
import com.coderdream.vo.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Login3Action extends ActionSupport implements ModelDriven<User> {
private User user = new User();
public User getModel() {
return user ;
}
public String execute() {
if ("aaa" .equalsIgnoreCase(user .getUsername().trim())
&& "123" .equalsIgnoreCase(user .getPassword().trim())) {
return SUCCESS ;
} else {
return INPUT ;
}
}
}
?
在jsp 中调用,不需要使用User 对象,直接通过属性名获取(输入):
代码清单 9 : login.jsp 片段
?
3 、使用 ModelDriven 模式接收用户输入:
< br />
< form action =" <%= path%> /login3.action" method ="POST" >
姓名:
< input type ="text" name ="username " >
< br />
密码:
< input type ="text" name ="password " >
< br />
< input type ="submit" name =" 登录 " >
< br />
</ form >
?
在jsp 中调用,不需要使用User 对象,直接通过属性名获取(输出):
代码清单 10 : login1Suc.jsp 片段
欢迎 < s:property value ="username " /> ,登录成功!
?