-------- Original Message --------
Subject: [MVC-Programmers] [Struts Tips] #3 - Use LookupDispatchAction 
for a JavaScript-free dispatch
Date: Thu, 28 Mar 2002 08:02:16 -0500
From: Ted Husted <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
Organization: Apache Software Foundation / Jakarta
Newsgroups: MVC-Programmers

Many forms can put to multiple uses. The fields we need to create a
record are usually the same fields we use to update a record. Likewise,
the same page can usually used to identify a record to delete, copy, or
update a record.

A DispatchAction (see Tip #2) is a convenient way to collect related
operations like this into a single Struts Action class. A field in the
request identifies the operation, and so all the developer has to do is
get the right value into the field. The simplest way to do this would be
to label the buttons for the operation, and give each button the same
name. But that ties the presentation label to the business operation and
confounds localization.

As an alternative, most developers use a JavaScript attached to the
button to update the dispatch field, which in practice works quite well.
If JavaScript is not enabled, the submit can fail gracefully, and in
practice most applications do rely JavaScript for essential operations.

If for any reason JavaScript is not an option, another good alternative
is the LookupDispatchAction [org.apache.struts.actions]. This object is
a little more work to setup on the server-side than the original
DispatchAction, but it lets you use the dispatch technique without a
JavaScript crutch.

As with the DispatchAction, the first step is to indicate the name of
the dispatch parameter in the action-mapping element. In this case the
parameter will the name given to each of the buttons, rather than a
hidden field. Let's just call our buttons "submit", which is the default
name the html:submit tag will use.

    <action path="/test"
            type="org.example.MyAction"
            name="MyForm"
           scope="request"
           input="/test.jsp"
       parameter="submit"/>

In our JSP, we can refer to the buttons in the usual way

    <html:form action="/test">
     <html:submit>
       <bean:message key="button.add"/>
     </html:submit>
     <html:submit>
       <bean:message key="button.delete"/>
     </html:submit>
   </html:form>

Later, when the user selects a button, the form will pass the submit
parameter, along with whatever message is Struts finds for "button.add"
or "button.delete" in the resource bundle. In a localized application,
this message would vary by the user's chosen locale.

Using the conventional DispatchAction, the plain button approach is
problematic, since there would have to a Java method named for each
message, and not all the messages might valid Java identifiers. But, one
developer's problem is another developer's opportunity, and the
LookupDispatchAction offers a deft solution to a thorny issue.

When you create your LookupDispatchAction subclass, along with the
methods for the dispatch operations (see Tip #20) 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;
   }

   public ActionForward add(ActionMapping mapping,
           ActionForm form,
           HttpServletRequest request,
           HttpServletResponse response)
           throws IOException, ServletException {
       // do add
       return mapping.findForward("success");
   }

   public ActionForward delete(ActionMapping mapping,
           ActionForm form,
           HttpServletRequest request,
           HttpServletResponse response)
           throws IOException, ServletException {
       // do delete
       return mapping.findForward("success");
   }

Internally, the base action will lookup the messages for button.add and
button.delete, and match those against the submit parameter. When it
finds a match, it will then use either "add" 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?

Most often not. In practice, you can easily use one dispatch action for
any forms that share a common validation. Sharing a dispatch action
between different form beans, or form beans that are validated
differently, 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.

HTH, Ted.

-----

Credits. The LookupDispatchAction was originated by Erik Hatcher, one of
Struts many volunteer contributors. The examples used in this tip relied
heavily Erik's documentation. (Yeah reuse! Yeah JavaDocs!)

-----

About Ted. Ted Husted is an active Struts Committer and moderates the
Struts mailing list and the JGuru Struts FAQ.

_______________________________________________
MVC-Programmers mailing list
[EMAIL PROTECTED]
http://www.basebeans.com:8081/mailman/listinfo/mvc-programmers

Reply via email to