Question

In what cases is better to use DispatchActions than Action?

Was it helpful?

Solution

When you need many similar actions with similar form bean in the same struts module (e.g. CRUD actions to create read update delete the same object). With plain Action you'll need 4 Struts action files with imports, headers, method signatures:

// 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
    }
}

Also you'll need 4 action mappings in struts-config.xml (of course if you are not using annotations). But in fact they will just invoke the next layer of code (manager/DAO/etc), which is independent from web (from request/response/mapping classes) and thus testable with unit tests allowing test-driven development and code reusability. All four classes will differ only in 1-2 lines of code. The rest is boilerplate, repeating again and again.

By adding additional parameters or reusing existing one in HTTP request (in other words: in JSP form tag) you can pack all 4 actions in one class, e.g. in 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
    }
}

This requires much less boilerplate. In order to tell which action you want to perform, you can use submit buttons:

<html:submit property="update" value="Save" />
<html:submit property="delete" value="Delete" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top