1.现在我们有EmployeeAction的Action类如下:
- package com.yu.struts2.app;
- import java.util.Map;
- import org.apache.struts2.interceptor.RequestAware;
- public class EmployeeAction implements RequestAware {
- private Integer employeeId;
- private Dao dao = new Dao();
- public String list(){
- requestMap.put("emps", dao.getEmployees());
- return "list";
- }
- public String delete(){
- dao.delete(employeeId);
- return "delete";
- }
- private String firstName;
- private String lastName;
- private String email;
- public String save(){
- Employee emp = new Employee(null, firstName, lastName, email);
- dao.save(emp);
- return "save";
- }
- private Map<String, Object> requestMap;
- @Override
- public void setRequest(Map<String, Object> arg0) {
- // TODO Auto-generated method stub
- this.requestMap = arg0;
- }
- public Integer getEmployeeId() {
- return employeeId;
- }
- public void setEmployeeId(Integer employeeId) {
- this.employeeId = employeeId;
- }
- public void setFirstName(String firstName){
- this.firstName = firstName;
- }
- public String getFirstName(){
- return this.fristName;
- }
- public void setLastName(String lastName){
- this.lastName = lastName;
- }
- public String getLastName(){
- return this.lastName;
- }
- public void setEmail(String email){
- this.email= email;
- }
- public String getLastName(){
- return this.lastName;
- }
- }
2.同时,我还编写了一个Employee这个类,如下:
- package com.yu.struts2.app;
- public class Employee {
- private Integer employeeId;
- private String firstName;
- private String lastName;
- private String email;
- public Integer getEmployeeId() {
- return employeeId;
- }
- public void setEmployeeId(Integer employeeId) {
- this.employeeId = employeeId;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public Employee(Integer employeeId, String firstName, String lastName, String email) {
- super();
- this.employeeId = employeeId;
- this.firstName = firstName;
- this.lastName = lastName;
- this.email = email;
- }
- public Employee() {
- super();
- }
- }
3.我们可以看出我现在save一个employee, 我在Action类中差不多实现了Employee类的所有成员和方法,我们可以看到Action类太过于繁琐,那么我们可不可以简化一下,把Action中涉及到的Employee类的成员变量和方法去掉,直接将值栈里面的属性值赋给一个Employee对象中,这样的话那么我们要使值栈的第一个对象栈必须是Employee对象,因此ModelDriven接口是处理这种问题解接口。
4.我们可以把EmployeeAction的类改写成:
- package com.yu.struts2.app;
- import java.util.Map;
- import org.apache.struts2.interceptor.RequestAware;
- import com.opensymphony.xwork2.ModelDriven;
- public class EmployeeAction implements RequestAware ,ModelDriven<Employee>{
- private Integer employeeId;
- private Dao dao = new Dao();
- public String list(){
- requestMap.put("emps", dao.getEmployees());
- return "list";
- }
- public String delete(){
- dao.delete(employee.getEmployeeId());
- return "success";
- }
- private Employee employee;
- public String save(){
- // 1.获取请求参数:通过定义对应属性的方式
- // 2.调用Dao的save方法
- dao.save(employee);
- return "success";
- }
- private Map<String, Object> requestMap;
- @Override
- public void setRequest(Map<String, Object> arg0) {
- // TODO Auto-generated method stub
- this.requestMap = arg0;
- }
- @Override
- public Employee getModel() {
- // TODO Auto-generated method stub
- employee = new Employee();
- return employee;
- }
- }
它的实现很简单,实现ModelDriven这个接口,并且通过getModel()方法得到Employee对象。
5.下面我们来具体分析下这个过程:
(1) 先执行ModelDrivenInterceptor的Intercept方法
代码分析:
- public String intercept(ActionInvocation invocation) throws Exception {
- // 获取Action对象:Employee对象,此时该Action已经实现了ModelDriven接口
- // public class EmployeeAction implements RequestAware ,ModelDriven<Employee>{
- Object action = invocation.getAction();
- // 判断action是否是ModelDriven的实例
- if (action instanceof ModelDriven) {
- // 强制转换为MOdelDriven类型
- ModelDriven modelDriven = (ModelDriven) action;
- // 获取值栈
- ValueStack stack = invocation.getStack();
- // 调用ModelDriven接口的getModel()方法
- // 即调用EmployeeAction的getModel()方法
- /*
- public Employee getModel() {
- employee = new Employee();
- return employee;
- }
- */
- Object model = modelDriven.getModel();
- if (model != null) {
- // 把getModel()方法返回值压入到值栈的栈顶。实际压入的是EmployeeAction的执行变量
- stack.push(model);
- }
- if (refreshModelBeforeResult) {
- invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
- }
- }
- return invocation.invoke();
- }
(2).执行ParametersInterceptor 的intercept方法:把请求参数的赋值给栈顶对象对应的属性。若栈顶对象没有对应的属性,则查询下一个对象对应的属性。。。
(3). 注意:getModel()方法不能提供以下实现.的确会返回一个 Employee对象到值栈的栈顶,但当前Action的employee成员变量却是null
public Employee getModel() {
//employee = new Employee();
return new Employee();
}