Programming Tutorials

Handling multiple buttons in HTML Form in Struts

By: Charles in Struts Tutorials on 2007-10-01  

The <html:submit> tag is used to submit the HTML form. The usage of the tag is as follows:

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

This will generate a HTML as follows.

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

This usually works okay if there was only one button with "real" Form submission (The other one maybe a Cancel button). Hence it suffices to straight away process the request in CustomerAction. However 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="CustomForm"action="/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 below 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.

// CustomerAction modified for multiple 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.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Struts )

Latest Articles (in Struts)