Handling multiple buttons in HTML Form in Struts
By: Charles
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.
Archived Comments
1. JasonNix
View Tutorial By: JasonNix at 2017-04-25 09:49:11
2. JasonNix
View Tutorial By: JasonNix at 2017-04-13 04:28:44
3. savannahstalmaiio
View Tutorial By: lindseycollenxi3 at 2017-03-28 10:49:51
4. magdaspotoh8l
View Tutorial By: thaddeusrobellantt at 2017-03-21 15:25:06
5. rodolfosandisonsvc
View Tutorial By: joshuabolognese1w8 at 2017-03-17 02:44:10
6. kathyrnalarid2my
View Tutorial By: louieneitzked8d at 2017-03-16 02:44:55
7. stepanieperrin1o1
View Tutorial By: hortensebogdanskinzi at 2017-03-15 17:57:20
8. sanjuanagompert3bm
View Tutorial By: dunglupinoprb at 2017-03-15 15:04:29
9. Awesome post....
View Tutorial By: Zeeshan Ali Ansari at 2012-11-24 06:35:20
10. great !!!!!!!!!!!!!!!!!!!!!
View Tutorial By: arjun at 2012-03-24 14:55:10
11. thanks for this blogs , i was looking for the same handling multiple submit buttons on the same page
View Tutorial By: prakash at 2011-11-27 18:25:32
12. @chintan Check this answer, that could help you. http://qa.java-samples.com/questions/2373/how-to-us
View Tutorial By: Mallika at 2011-10-06 11:59:45
13. HI, Thanks for your beautiful tutorial.
I have two buttons in one form and having two
View Tutorial By: Chintan at 2011-10-06 07:35:01
14. Very useful tutorial. But if same code is executed for both the buttons in the struts-config file th
View Tutorial By: Jitendra Kumar Mahto at 2011-08-12 15:10:14
15. thanks for this tutorial! it's very useful
View Tutorial By: Miguel at 2011-06-26 21:15:00
16. Thank you so much. I have migrated from the .NET to the J2EE platform and I was missing the Event Mo
View Tutorial By: kanika at 2011-05-31 12:23:33
17. Thanks for the solution. I must say however that this solution works ALL the time, including interna
View Tutorial By: Pierre at 2009-11-19 20:16:35
18. To handle multiple submit buttons you can use standard Struts1 classes DispatchAction EventDispatchA
View Tutorial By: Vic at 2008-12-23 12:03:43
19. safsdfs
View Tutorial By: nmn at 2008-09-09 01:07:49
20. hello sir
i am simulating railway reservations system using servlets technology.
View Tutorial By: karthick at 2008-08-30 03:09:42
21. farhaan, thats why I said, "Using the HTML Button Label to distinguish the buttons works for mo
View Tutorial By: Charles at 2008-03-21 01:36:46
22. This tutorial is very good. Actually I have been searching the solution for a such a problem that yo
View Tutorial By: farhaan at 2008-03-20 15:22:13
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Configuring JDBC DataSources in Struts
When is the best time to validate input in Struts
Simple example of using the requiredif Validator rule in Struts
How to prepopulate a form in Struts
Using JavaScript to submit a form in Struts
FAQ: Why are my checkboxes not being set from ON to OFF?
FAQ: Why was reload removed from Struts (since 1.1)?
What is a Plug-in and how to use Java plug-ins with Struts?
Origin and Architecture of Struts
Handling multiple buttons in HTML Form in Struts