Handling multiple buttons in HTML Form


Handling multiple buttons in HTML Form

In the example application, we used the < html:submit > tag to submit the HTML form. Our usage of the tag was as follows :

 <html:submit><bean:message key=button.save/></html:submit> 

This generated a HTML as follows.

 <input type="submit" value="Save Me"> 

This worked okay for us since there was only one button with ‚“real ‚½ Form submission (The other one was a Cancel button). Hence it sufficed for us to straight away process the request in CustomerAction . You will frequently face situations where there are more than one or two buttons submitting the form. You would want to execute different code based on the buttons clicked. If you are thinking, ‚“No problem. I will have different ActionMapping (and hence different Actions) for different buttons ‚½, you are out of luck! Clicking any of the buttons in a HTML Form always submits the same Form, with the same URL. The Form submission URL is found in the action attribute of the form tag as:

 <formname=CustomFormaction=/App1/submitCustomerForm.do/> 

and is unique to the Form. You have to use a variation of the < html:submit > as shown below to tackle this problem.

 <html:submit property=step> <bean:message key=button.save/>            </html:submit> 

The above SubmitTag, has an additional attribute named property whose value is step . The meaning of the property attribute is similar to that in < html:text > - It represents a JavaBeans property in the ActionForm and generates the name of the Form input element. This tag generates a HTML as follows

 <input type="submit" name=step value="Save Me"> 

The generated HTML submit button has a name associated with it. You have to now add a JavaBeans property to your ActionForm whose name matches the submit button name. In other words an instance variable with a getter and setter are required. If you were to make this change in the application just developed, you have to add a variable named ‚“ step ‚½ in the CustomerForm and then add two methods getStep() and setStep() . The Struts Framework sets the value of the step by Introspection, just like it does on the other fields. In the CustomerAction , the logic corresponding to the Save Me button is executed after performing a check for the Save Me button. Listing 3.12 shows the modified execute() method from CustomerAction . The changes are shown in bold. When the Save Me button is pressed, the custForm.getStep() method returns a value of ‚“Save Me ‚½ and the corresponding code block is executed.

Listing 3.12: CustomerAction modified for mutltiple button Forms
 public class CustomerAction extends Action {    public ActionForward execute(ActionMapping mapping,              ActionForm form, HttpServletRequest request,              HttpServletResponse response) throws Exception    {       if (isCancelled(request)) {          System.out.println(Cancel Operation Performed");          return mapping.findForward("mainpage");       }       CustomerForm custForm = (CustomerForm) form;       ActionForward forward = null;  if ("Save Me".equals(custForm.getStep())) {   System.out.println("Save Me Button Clicked");  String firstName = custForm.getFirstName();           String lastName = custForm.getLastName();           System.out.println("Customer First name is " +                                             firstName);           System.out.println("Customer Last name is " +                                             lastName);           forward = mapping.findForward("success");  }  return forward;    } } 
 

In Struts applications, when using regular buttons, it is customary for all submit buttons to have the same name (except Cancel and Reset buttons). This is for convenience purposes. In HTML, when a form is submitted, only one of the submit buttons is pressed and hence only the value of that button is submitted. The ActionForm can thus have a single instance variable for all the submit buttons in its Form. This makes the if-else check in the Action class easier. Suppose that the HTML Customer Form that we show to the users has another button with label ‚“Spike Me ‚½. The submit button can still have the name ‚“ step ‚½ (same as the ‚“Save Me ‚½ button). This means the CustomerForm class has a single JavaBeans property ‚“ step ‚½ for the submit buttons. In the CustomerAction you can have check if the custForm.getStep() is ‚“ Save Me ‚½ or ‚“ Spike Me ‚½. If each of the buttons had different names like button1, button2 etc. then the CustomerAction would have to perform checks as follows:

 if ("Save Me".equals(  custForm.getButton1()  ) {             //  Save Me Button pressed         } else if ("Spike Me".equals(  customForm.getButton2()  ) {             // Spike Me button pressed         } 

Using the HTML Button Label to distinguish the buttons works for most of the cases except when you have a internationalized Struts web application. Consider the HTML rendered for a Spanish user . By virtue of the Message Resource Bundles (< bean:message > tag), the Spanish user will see a label of ‚“ Excepto M ƒ ­ ‚½ instead of ‚“ Save Me ‚½. However the CustomerAction class is still looking for the hard coded ‚“ Save Me ‚½. Consequently the code block meant for ‚“ Save Me ‚½ button never gets executed. In Chapter 4, you will see how a specialized subclass of the Action called LookupDispatchAction solves this problem.




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