LookupDispatchAction

 < Day Day Up > 



The LookupDispatchAction is a lot like the DispatchAction (which it subclasses). It does a reverse lookup against the resource bundle. You have to implement a special method that maps the message resource keys to the methods you want to invoke.

To use the LookupDispatchAction, perform the following steps:

  1. Create an action handler class that subclasses LookupDispatchAction.

  2. Create a method to represent each logical related action.

  3. Implement the getKeyMethodMap method to map the resource keys to method names.

  4. Create an action mapping for this action handler using the parameter attribute to specify the request parameter that carries the name of the method you want to invoke.

  5. Set up the messages in the resource bundle for the labels and values of the buttons.

  6. Use bean:message to display the labels of the button in the bean.

The first step is to create an action handler class that subclasses LookupDispatchAction:

 public class UserLookupDispatchAction extends                                      LookupDispatchAction { } 

Next, you create a method to represent each logical related action:

 public class UserLookupDispatchAction extends                                      LookupDispatchAction {      ...      public ActionForward remove(           ActionMapping mapping,           ActionForm form,           HttpServletRequest request,           HttpServletResponse response)           throws Exception {                System.out.println("REMOVE USER (LOOKUP)");                return mapping.findForward("success"); } public ActionForward save(           ActionMapping mapping,           ActionForm form,           HttpServletRequest request,           HttpServletResponse response)           throws Exception {                System.out.println("SAVE USER (LOOKUP)");                return mapping.findForward("success");      }      ... } 

Notice these methods have the same signature of the standard Action.execute method (except for the method name).

Third, you must implement the getKeyMethodMap method to map the resource keys to method names:

 public class UserLookupDispatchAction extends                                      LookupDispatchAction {      protected Map getKeyMethodMap() {           Map map = new HashMap();           map.put("userForm.remove", "remove");           map.put("userForm.save", "save");           return map;      }      ... } 

Next, create an action mapping for this action handler using the parameter attribute to specify the request parameter that carries the name of the method you want to invoke:

           <action               path="/lookupDispatchUserSubmit"               type="action.UserLookupDispatchAction"               input="/form/userForm.jsp"               name="userForm"               parameter="method"               scope="request"               validate="true">               <forward name="success" path="/success.jsp" />           </action> 

The fifth step is to set up the messages in the resource bundle for the labels and values of the buttons. Inside your resource bundle (e.g., application.properties), add the following two entries:

 userForm.save=Save userForm.remove=Remove 

Finally, use bean:message to display the labels of the button in the bean:

 <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> ...      <html:link action="home">Home </html:link>           <html:form action="/lookupDispatchUserSubmit">           ...                <html:submit property="method">                <bean:message key="userForm.remove"/>                </html:submit>                <html:submit property="method">                <bean:message key="userForm.save"/>                </html:submit>                <html:cancel>                </html:cancel>           </html:form>      <body> </html> 

When the user clicks the Remove button on the HTML form, the remove method is called on the action. When the user clicks the Save button, the save method is called on the action.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

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