Use LookupDispatchAction for a JavaScript-free dispatch

类别:Java 点击:0 评论:0 推荐:
Use LookupDispatchAction for a JavaScript-free dispatch

Many forms can be 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 is 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 fails gracefully, and in practice most applications do rely JavaScript for essential operations. 

If JavaScript is not an option, another good alternative is the LookupDispatchAction [org.apache.struts.actions.LookupDispatchAction]. This object is a little more work to setup that the original DispatchAction, but lets you use this technique without using JavaScript as a 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 could vary by the user's selected locale. 

Using the conventional DispatchAction, this approach would problematic, since there would have to a Java method named for each message, and not all the messages might valid Java identifiers. 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 (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 on Erik's documentation. (Yeah reuse! Yeah JavaDocs!)

本文地址:http://com.8s8s.com/it/it13911.htm