struts2获取前台传递过来的数据可以通过属性驱动和模型驱动两种方式获得。
属性驱动
1、使用变量的方式
前台:
<form action="mylogin_login" method="post">
<table>
<tr>
<td>account:</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="login"></td>
</tr>
</table>
</form>
action:
public class LoginAction{
private String account;
private String password;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login(){
//如果账号是lili,密码是123456就可以去index页面
if("lili".equals(account) && "123456".equals(password)){
return "loginSuccess";
}
//否则继续回到login页面
return "login";
}
}
action中要获得前台传递过来的account和password两个参数的数据。那么就必须在action中设置和前台name属性值同样名称的变量。同时必须设置get、set方法。这样在访问action的过程中,struts会自动的为action中的这两个变量设置前台传过来的值
也就是说把参数作为Action的类属性,并且提供属性的get、set方法时,xwork的OGNL会自动的把request参数的值设置到类属性中,此时访问请求参数只需要访问类属性即可。
2、使用对象的方式
使用变量的方式会有一个缺点,就是当你的表单要传递的数据量非常多的时候,好几十个,你不可能在action中定义好几十个变量,并且设置它们的get和set方法吧。
这个时候我们就需要通过在action中设置一个module对象即实体对象,并且设置这个对象的get、set方法即可。
前台:这里的要传递的参数的name属性值必须通过变量.属性值的形式进行设置才可以获得到
<form action="mylogin_login" method="post">
<table>
<tr>
<td>account:</td>
<td><input type="text" name="user.account"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="login"></td>
</tr>
</table>
</form>
user类:
public class User {
private String account;
private String password;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
action:
public class LoginAction {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String login() {
// 如果账号是lili,密码是123456就可以去index页面
if ("lili".equals(user.getAccount()) && "123456".equals(user.getPassword())) {
return "loginSuccess";
}
// 否则继续回到login页面
return "login";
}
}
可以发现,我们前台页面的name属性值都变成了user.account和user.password,我们通过前台传递过来的user对象在action界面得到对应的属性值。通过封装的方式来实现前台的值传递到action
模型驱动
需要当前action实现com.opensymphony.xwork2.ModelDriven接口,并重写 getModel方法才能获取表单中的封装对象。并且声明对象的时候一定要实例化,但不需要get和set方法。
前端界面:可以发现name属性可以直接指定对象的属性值,并且必须对应才可以获得到。
<form action="mylogin_login" method="post">
<table>
<tr>
<td>account:</td>
<td><input type="text" name="account"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="login"></td>
</tr>
</table>
</form>
action:需要初始化user类,不需要set和get方式即可
public class LoginAction implements ModelDriven<User> {
private User user = new User();
@Override
public User getModel() {
return user;
}
public String login() {
// 如果账号是lili,密码是123456就可以去index页面
if ("lili".equals(user.getAccount()) && "123456".equals(user.getPassword())) {
return "loginSuccess";
}
// 否则继续回到login页面
return "login";
}
}