一、DomainModel(域模型)
1、
应用场景:一般我们在struts2的action中接收参数通常是如下方式
package cn.orlion.user; import com.opensymphony.xwork2.ActionSupport; public class UserAction 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 login(){ System.out.println("username=" + username); System.out.println("password=" + password); return SUCCESS; } }
这样会产生一个问题,当我们要获取的属性增多的话比如我们要加上age、sex、birthday…等等这样我们就要写巨多的getset方法。显然不可取,所以这时就可以用DomainModel来接收参数。
首先创建一个包:cn.orlion.model,然后创建一个User类,如下:
package cn.orlion.model; public class User { private String username; private String password; private String age; 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 getAge() { return age; } public void setAge(String age) { this.age = age; } }
将上边的UserAction改成:
package cn.orlion.user; import com.opensymphony.xwork2.ActionSupport; import cn.orlion.model.User; public class UserAction extends ActionSupport{ private User user; public String login(){ System.out.println("username=" + user.getUsername()); System.out.println("password=" + user.getPassword()); System.out.println("age=" + user.getAge()); return SUCCESS; } public User getUser(){ return user; } public void setUser(User user){ this.user = user; } }
当访问
http://localhost:8080/Struts2Demo1/user/login.action?user.username=1&user.password=2&user.age=3时就会在控制台打印出username=1
password=2
age=3
2、DTO(Data transfer Object)数据传输对象
上边的代码已经很好的解决了属性过多的问题,但仍然存在一个问题:当我们有一个属性不能添加到user中时,(比如注册页面通常会有一个确认密码的输入框用来接收passwordConfim)怎么办?这时就可以用DTO解决。由DTO生成域对象。
首先创建一个包cn.orlion.dto,然后创建一个类UserDTO如下:
package cn.orlion.dto; public class UserDTO { private String username; private String password; private String passwordConfirm; private String age; public String getAge() { return age; } public void setAge(String age) { this.age = age; } 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 getPasswordConfirm() { return passwordConfirm; } public void setPasswordConfirm(String passwordConfirm) { this.passwordConfirm = passwordConfirm; } }
然后UserAction改为:
package cn.orlion.user; import com.opensymphony.xwork2.ActionSupport; import cn.orlion.dto.UserDTO; import cn.orlion.model.User; public class UserAction extends ActionSupport{ private UserDTO userdto; public String login(){ String password = userdto.getPassword(); String passwordConfirm = userdto.getPasswordConfirm(); System.out.println(password + "-" + passwordConfirm); if (!password.equals(passwordConfirm)) { System.out.println("两次输入密码不一致"); return ERROR; } User user = new User(); user.setUsername(userdto.getUsername()); user.setPassword(userdto.getPassword()); user.setAge(userdto.getAge()); // 添加用户操作... return SUCCESS; } public UserDTO getUserdto() { return userdto; } public void setUserdto(UserDTO userdto) { this.userdto = userdto; } }
访问http://localhost:8080/Struts2Demo1/user/login.action?userdto.username=1&userdto.password=2&userdto.passwordConfirm=2&userdto.age=3
这样就可以了。
二、ModelDriven
这种方式是让Action类实现ModelDriven接口:
package cn.orlion.user; import com.opensymphony.xwork2.ModelDriven; import cn.orlion.model.User; public class UserAction implements ModelDriven<User>{ private User user; public String login(){ System.out.println(user.getUsername()); System.out.println(user.getPassword()); return "success"; } public User getUser(){ return user; } public void setUser(User user){ this.user = user; } @Override
// 由于实现接口泛型指定为User所以返回值类型就不是Object而是User了。 public User getModel() { return user; } }
这样当访问http://localhost:8080/Struts2Demo/user/login.action?user.username=1&user.password=2控制台就会打印1、2。