【问题标题】:Struts 1.3: when is better to use DispatchActions than Action?Struts 1.3:什么时候使用 DispatchActions 比使用 Action 更好?
【发布时间】:2012-11-15 18:55:57
【问题描述】:

在什么情况下使用 DispatchActions 比使用 Action 更好?

【问题讨论】:

    标签: action struts-1 dispatch struts1


    【解决方案1】:

    当您需要在同一个 struts 模块中使用类似表单 bean 的许多类似操作时(例如,对 create read update delete 同一个对象的 CRUD 操作)。使用普通的Action,您将需要 4 个 Struts 动作文件,其中包含导入、标头、方法签名:

    // CreateAction.java
    package com.example.package;
    // imports and header
    public class CreateAction extends Action {
        public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    }
    
    // ReadAction.java
    package com.example.package;
    // imports and header
    public class ReadAction extends Action {
        public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    }
    
    // UpdateAction.java
    package com.example.package;
    // imports and header
    public class UpdateAction extends Action {
        public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    }
    
    // DeleteAction.java
    package com.example.package;
    // imports and header
    public class DeleteAction extends Action {
        public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    }
    

    您还需要struts-config.xml 中的 4 个操作映射(当然,如果您不使用annotations)。但实际上它们只会调用下一层代码(manager/DAO/etc),它独立于 Web(来自请求/响应/映射类),因此可以通过单元测试进行测试,从而实现测试驱动开发和代码可重用性。所有四个类仅在 1-2 行代码上有所不同。其余的都是样板文件,一次又一次地重复。

    通过在 HTTP 请求中添加额外的参数或重用现有的参数(换句话说:在 JSP 表单标签中),您可以将所有 4 个操作打包在一个类中,例如在EventDispatchAction:

    // CRUDAction.java
    package com.example.package;
    // imports and header
    public class CRUDAction extends EventDispatchAction {
        public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    
        public ActionForward read(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    
        public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    
        public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
            // actual code
        }
    }
    

    这需要更少的样板。为了告诉您要执行哪个操作,您可以使用提交按钮:

    <html:submit property="update" value="Save" />
    <html:submit property="delete" value="Delete" />
    

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 2013-10-31
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多