Handling multiple buttons in HTML Form in Struts

By: Charles Viewed: 169 times  Printer Friendly Format    


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.



Most Viewed Articles (in Struts )

Latest Articles (in Struts)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. hello sir

i am simulating railway r

View Tutorial          By: karthick at 2008-08-30 03:09:42

2. safsdfs
View Tutorial          By: nmn at 2008-09-09 01:07:49

3. farhaan, thats why I said, "Using the HTML Bu
View Tutorial          By: Charles at 2008-03-21 01:36:46

4. This tutorial is very good. Actually I have been s
View Tutorial          By: farhaan at 2008-03-20 15:22:13

5. To handle multiple submit buttons you can use stan
View Tutorial          By: Vic at 2008-12-23 12:03:43

6. Thanks for the solution. I must say however that t
View Tutorial          By: Pierre at 2009-11-19 20:16:35

7. Thank you so much. I have migrated from the .NET t
View Tutorial          By: kanika at 2011-05-31 12:23:33

8. thanks for this tutorial! it's very useful
View Tutorial          By: Miguel at 2011-06-26 21:15:00

9. Very useful tutorial. But if same code is executed
View Tutorial          By: Jitendra Kumar Mahto at 2011-08-12 15:10:14

10. HI, Thanks for your beautiful tutorial.

View Tutorial          By: Chintan at 2011-10-06 07:35:01

11. @chintan Check this answer, that could help you. h
View Tutorial          By: Mallika at 2011-10-06 11:59:45

12. thanks for this blogs , i was looking for the same
View Tutorial          By: prakash at 2011-11-27 18:25:32

13. great !!!!!!!!!!!!!!!!!!!!!
View Tutorial          By: arjun at 2012-03-24 14:55:10

14. Awesome post....
View Tutorial          By: Zeeshan Ali Ansari at 2012-11-24 06:35:20

15. sanjuanagompert3bm
View Tutorial          By: dunglupinoprb at 2017-03-15 15:04:29

16. stepanieperrin1o1
View Tutorial          By: hortensebogdanskinzi at 2017-03-15 17:57:20

17. kathyrnalarid2my
View Tutorial          By: louieneitzked8d at 2017-03-16 02:44:55

18. rodolfosandisonsvc
View Tutorial          By: joshuabolognese1w8 at 2017-03-17 02:44:10

19. magdaspotoh8l
View Tutorial          By: thaddeusrobellantt at 2017-03-21 15:25:06

20. savannahstalmaiio
View Tutorial          By: lindseycollenxi3 at 2017-03-28 10:49:51

21. JasonNix
View Tutorial          By: JasonNix at 2017-04-13 04:28:44

22. JasonNix
View Tutorial          By: JasonNix at 2017-04-25 09:49:11