Struts2的学习(3)-ModelDriven接口的应用

1.现在我们有EmployeeAction的Action类如下:
  1. package com.yu.struts2.app;

  2. import java.util.Map;

  3. import org.apache.struts2.interceptor.RequestAware;

  4. public class EmployeeAction implements RequestAware {

  5.     private Integer employeeId;

  6.     private Dao dao = new Dao();

  7.     public String list(){
  8.         requestMap.put("emps", dao.getEmployees());
  9.         return "list";
  10.     }

  11.     public String delete(){
  12.         dao.delete(employeeId);
  13.         return "delete";
  14.     }

  15.     private String firstName;
  16.     private String lastName;
  17.     private String email;

  18.     public String save(){
  19.     Employee emp = new Employee(null, firstName, lastName, email);
  20.     dao.save(emp);
  21.     
  22.     return "save";
  23.     }

  24.     private Map<String, Object> requestMap;

  25.     @Override
  26.     public void setRequest(Map<String, Object> arg0) {
  27.         // TODO Auto-generated method stub
  28.         this.requestMap = arg0;
  29.     }

  30.     public Integer getEmployeeId() {
  31.         return employeeId;
  32.     }

  33.     public void setEmployeeId(Integer employeeId) {
  34.         this.employeeId = employeeId;
  35.     }

  36.     public void setFirstName(String firstName){
  37.         this.firstName = firstName;
  38.     }

  39.     public String getFirstName(){
  40.         return this.fristName;
  41.     }

  42.     
  43.     public void setLastName(String lastName){
  44.         this.lastName = lastName;
  45.     }

  46.     public String getLastName(){
  47.         return this.lastName;
  48.     }

  49.     
  50.     public void setEmail(String email){
  51.         this.emailemail;
  52.     }

  53.     public String getLastName(){
  54.         return this.lastName;
  55.     }

  56. }
2.同时,我还编写了一个Employee这个类,如下:
  1. package com.yu.struts2.app;

  2. public class Employee {

  3.     private Integer employeeId;
  4.     private String firstName;
  5.     private String lastName;

  6.     private String email;

  7.     public Integer getEmployeeId() {
  8.         return employeeId;
  9.     }

  10.     public void setEmployeeId(Integer employeeId) {
  11.         this.employeeId = employeeId;
  12.     }

  13.     public String getFirstName() {
  14.         return firstName;
  15.     }

  16.     public void setFirstName(String firstName) {
  17.         this.firstName = firstName;
  18.     }

  19.     public String getLastName() {
  20.         return lastName;
  21.     }

  22.     public void setLastName(String lastName) {
  23.         this.lastName = lastName;
  24.     }

  25.     public String getEmail() {
  26.         return email;
  27.     }

  28.     public void setEmail(String email) {
  29.         this.email = email;
  30.     }

  31.     public Employee(Integer employeeId, String firstName, String lastName, String email) {
  32.         super();
  33.         this.employeeId = employeeId;
  34.         this.firstName = firstName;
  35.         this.lastName = lastName;
  36.         this.email = email;
  37.     }

  38.     public Employee() {
  39.         super();
  40.     }

  41. }

3.我们可以看出我现在save一个employee, 我在Action类中差不多实现了Employee类的所有成员和方法,我们可以看到Action类太过于繁琐,那么我们可不可以简化一下,把Action中涉及到的Employee类的成员变量和方法去掉,直接将值栈里面的属性值赋给一个Employee对象中,这样的话那么我们要使值栈的第一个对象栈必须是Employee对象,因此ModelDriven接口是处理这种问题解接口。
4.我们可以把EmployeeAction的类改写成:
  1. package com.yu.struts2.app;

  2. import java.util.Map;

  3. import org.apache.struts2.interceptor.RequestAware;

  4. import com.opensymphony.xwork2.ModelDriven;

  5. public class EmployeeAction implements RequestAware ,ModelDriven<Employee>{

  6.     private Integer employeeId;

  7.     private Dao dao = new Dao();

  8.     public String list(){
  9.         requestMap.put("emps", dao.getEmployees());
  10.         return "list";
  11.     }

  12.     public String delete(){
  13.         dao.delete(employee.getEmployeeId());
  14.         return "success";
  15.     }

  16.     private Employee employee;

  17.     public String save(){
  18.         // 1.获取请求参数:通过定义对应属性的方式
  19.         // 2.调用Dao的save方法
  20.         dao.save(employee);

  21.         return "success";
  22.     }

  23.     private Map<String, Object> requestMap;

  24.     @Override
  25.     public void setRequest(Map<String, Object> arg0) {
  26.         // TODO Auto-generated method stub
  27.         this.requestMap = arg0;
  28.     }

  29.     @Override
  30.     public Employee getModel() {
  31.         // TODO Auto-generated method stub
  32.         employee = new Employee();
  33.         return employee;
  34.     }
  35. }
它的实现很简单,实现ModelDriven这个接口,并且通过getModel()方法得到Employee对象。

5.下面我们来具体分析下这个过程:
(1) 先执行ModelDrivenInterceptor的Intercept方法
代码分析:
  1.  public String intercept(ActionInvocation invocation) throws Exception {
  2.     // 获取Action对象:Employee对象,此时该Action已经实现了ModelDriven接口
  3.     // public class EmployeeAction implements RequestAware ,ModelDriven<Employee>{
  4.         Object action = invocation.getAction();
  5.     
  6.     // 判断action是否是ModelDriven的实例
  7.         if (action instanceof ModelDriven) {
  8.             // 强制转换为MOdelDriven类型
  9.             ModelDriven modelDriven = (ModelDriven) action;
  10.             // 获取值栈
  11.             ValueStack stack = invocation.getStack();
  12.             // 调用ModelDriven接口的getModel()方法
  13.             // 即调用EmployeeAction的getModel()方法
  14.             /*
  15.             public Employee getModel() {
  16.             employee = new Employee();
  17.             return employee;
  18.             }
  19.             */
  20.             Object model = modelDriven.getModel();
  21.             if (model !=  null) {
  22.                 // 把getModel()方法返回值压入到值栈的栈顶。实际压入的是EmployeeAction的执行变量
  23.                 stack.push(model);
  24.             }
  25.             if (refreshModelBeforeResult) {
  26.                 invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
  27.             }
  28.         }
  29.         return invocation.invoke();
  30.     }

(2).执行ParametersInterceptor 的intercept方法:把请求参数的赋值给栈顶对象对应的属性。若栈顶对象没有对应的属性,则查询下一个对象对应的属性。。。
(3). 注意:getModel()方法不能提供以下实现.的确会返回一个 Employee对象到值栈的栈顶,但当前Action的employee成员变量却是null
 public Employee getModel() {
            //employee = new Employee();
            return new Employee();
  }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值