DispatchAction for Image Button form submissions


DispatchAction and LookupDispatchAction work by invoking a method on the Action whose name matches a predefined request parameter. This works fine for form submissions when all the buttons have the same name but does not work for image button form submissions. In this section, we will further customize the ActionMapping to support a DispatchAction like feature for image based form submissions. This can be used in an enterprise application without a second thought. It will definitely prove useful timesaver.

As before, a new < set-property > needs to be added to the struts-config.xml as follows :

 <set-property property="methods"                   value="doNext,saveCustInfo,cancelTx" /> 

This setting works in conjunction with the < set-property > for buttons property in MyActionMapping . The methods is a comma-separated list of method names to be invoked for every button name defined in the comma-separated buttons < set-property >. A subclass of MybankBaseAction called MyDispatchAction is created to provide the DispatchAction-like features. This class has concrete implementation for MybankBaseAction ‚ s process() method. To use this class, you should subclass the MyDispatchAction . At runtime, the MyDispatchAction invokes appropriate method from your subclass via reflection. The process() method is shown in Listing 10.8.

Listing 10.8: Base Action class with DispatchAction like features
 public class MyDispatchAction extends MybankBaseAction {    protected ActionForward process(MyActionMapping mapping,              MybankBaseForm form, HttpServletRequest request,                 HttpServletResponse response) throws Exception     {       ActionForward forward = null;       String selectedButton =                     getSelectedButton(myForm, mapping)       String methodName = mapping.getMethod(button);       Object[] args = {mapping, form, request, response};       // this invokes the appropriate method in subclass       forward = (ActionForward)            MethodUtils.invokeMethod(this, ,methodName, args);       return forward;    } } 
 

The underlying logic is almost similar to previous utilities. In the process() method, the method to be invoked for the currently selected button is retrieved from MyActionMapping . Then, using the MethodUtils (another helper class from BeanUtils package), the actual method is invoked. The actual method name to be invoked is specified in the action mapping. These methods are similar to any method you would write had it been a regular DispatchAction. The methods have the fixed signature:

 public ActionForward methodName(MyActionMapping mapping,              MybankBaseForm form, HttpServletRequest request,                 HttpServletResponse response) throws Exception 



Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net