Header Ads

  • Breaking Now

    What is DispatchAction?

    org.apache.struts.actions.DispatchAction is responsible for

    -Dispatches to a public method named on a request parameter
    -Method name corresponds to the 'parameter' property of corresponding ActionMapping
    -useful when multiple similar actions are to be clubbed within a singe Action class in order to simplify the design.

    If you want to to insert,update and delete all actions on a database from a JSP with the same Action class in such case it will come quite handy.
    Here is how this JSP looks like:
    <html:form action="/saveSubscription">
    <html:submit>
    <bean:message key="insert"/>
    </html:submit>
    <html:submit>
    <bean:message key="update"/>
    </html:submit>
    <html:submit>
    <bean:message key="delete"/>
    </html:submit>
    </html:form>
    To configure the use of this action in your struts-config.xml file, create an entry like this:

    <action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="submit"/>

    It will use the value of the request parameter named "submit" to pick the appropriate "execute" method, which must have the same signature (other than method name) of the standard Action.execute method. For example, you might have the following three methods in the same action:

    * public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    * public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    * public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

    In our JSP, we can refer to the buttons in the usual way.Later, when the user selects a button, the form will pass the submit parameter, along with whatever message is Struts finds for "insert" or "delete" in the resource bundle.
    Using the conventional DispatchAction, this approach would be problematic, not necessarily each message will map to a valid Java identifier. This is where the magic of the LookupDispatchAction comes into play.

    When you create your LookupDispatchAction subclass, along with the methods for the dispatch operations you must also implement a getKeyMethodMap method. This is a "hotspot" that the LookupDispatchAction will call.

    Here's an example of the methods you might declare in your subclass:

    protected Map getKeyMethodMap(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request) {
    Map map = new HashMap();
    map.put("button.add", "add");
    map.put("button.delete", "delete");
    return map;
    }

    Internally, the base action will lookup the messages for 'insert' and 'delete', and match those against the submit parameter. When it finds a match, it will then use either "insert" or "delete" to call the corresponding methods.

    So while the LookupDispatchAction means adding an extra method to your Action, it lets you skip putting a JavaScript in your form.

    Both the DispatchAction and LookupDispatchAction are an excellent way to streamline your Struts action classes, and group several related operations into a single umbrella action. So, how many dispatch actions do you need? Can you use a dispatch action to collect everything into a single action?

    In practice, you can easily use one dispatch action for any forms that share a common validation. It is not advisable to have sharing of dispatch action between different form beans, or form beans that are validated differently,as it can start to make things harder rather than simpler. But the use of a dispatch action can easily half or quarter the number of action classes in most Struts application.

    Post Top Ad

    Post Bottom Ad