Using JavaScript to submit a form in Struts

By: Apache Foundation Emailed: 1744 times Printed: 2432 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

You can submit a form with a link as below. BTW, the examples below assume you are in an <html:form> block and 'myForm' is picked up from the struts-config.xml name field of the action.


    <a href='javascript:void(document.forms["myForm"].submit()>My Link</a>

Now the trick in the action is to decode what action you intend to perform. Since you are using JavaScript, you could set a field value and look for it in the request or in the form.

... html/javascript part ...


        <input type='hidden' value='myAction' />
        <input type='button' value='Save Meeeee'
            onclick='document.forms["myForm"].myAction.value="save";
                     document.forms["myForm"].submit();' />
        <input type='button' value='Delete Meeeee'
            onclick='document.forms["myForm"].myAction.value="delete";
                     document.forms["myForm"].submit();' />

... the java part ...


        class MyAction extends ActionForm implements Serializable {

            public ActionForward execute (ActionMapping map, ActionForm form,
                HttpServletRequest req, HttpServletResponse) {

                    String myAction = req.getParameter("myAction");

                    if (myAction.equals("save") {
                           // ...  save action  ...
                    } else if (myAction.equals("delete") {
                           // ...  delete action  ...
                    }
                }
            }
        }

This is just one of many ways to achieve submitting a form and decoding the intended action. Once you get used to the framework you will find other ways that make more sense for your coding style and requirements. Just remember this example is completely non-functional without JavaScript.


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

how do i write the above same code using ajax and jquery?

View Tutorial          By: Karthik at 2011-08-29 11:21:45
2. View Comment

nice

View Tutorial          By: srfds at 2012-05-28 09:25:15

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Apache Foundation
When is the best time to validate input in Struts
Chaining actions 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)?

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Struts 1 vs Struts 2
ActionErrors and ActionError in Struts
Handling Duplicate Form Submissions in Struts
What is Struts? Which Version of Struts to use?
ForwardAction in Struts
Configuring JDBC DataSources in Struts
Handling multiple buttons in HTML Form in Struts
IncludeAction in Struts
Using JavaScript to submit a form in Struts
Struts and Tiles - Steps to use Struts and Tiles
MVC Architecture (Model 2 Architecture)
Editing struts-config.xml in a Struts Application
Introduction to Struts Architecture
Installing and configuring Tomcat and Struts
Most Emailed Articles (in Struts)
Guidelines for Struts Application Development
WebWork 2 is now Apache Struts 2
Downloading and installing Struts
Struts 1 vs Struts 2
The directories and files of a Struts application
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
Is Struts the most popular web application framework for Java?
What is a Model View Controller (MVC) Model?
History of Struts
Creating Struts Modules
Using Multibox in Struts
ForwardAction in Struts
What is Struts? Which Version of Struts to use?
Editing web.xml in a Struts Application