The Kat's Work - Blog
Main | Blog | Registration | Login
11:52 PM
Welcome Guest | RSS
Main » 2011 » February » 14 » Handaling multiple submit Buttons in Java Spring.
3:14 PM
Handaling multiple submit Buttons in Java Spring.
Very simple this one but one that has very little support online. In fact has the opposite of support as people suggest truely awful solutions involving javascript or multiple forms.

So here is the situation. You have a form you want two submit buttons, lets say a "Send" and a "Save as Draft" button.

The html page doesn't care what your going to do with the info on the form it just wants to submit it. Your action is awaiting the same information independant of which button is pressed. It perfoms almost the same processes on the data the only difference is that if Send is pressed the form goes through a final submission process. In this example this involves setting a flag on a table to true.

As you can see the multiple forms "solution" isn't an option as the same fields need to be submitted independant of the which one is pressed, and the javascript "solution" is just rubbish. Come on call yourself a programmer.

In this case if we set up the html form correctly it will pass a parameter through that will let our action now which button was pressed.

To set up the form we need to add an id, name and value to each submit button as below.

<input id="save" type="submit" value="save" name="save"/>
<input id="send" type="submit" value="send" name="send"/>

Now when one of these buttons is pressed it will add a parameter of the that name (save or send) to the post request.

In your action you simply have to test for them. As below.

if( request.getParameter("save") != null )
        //Save specific processing
        else if( request.getParameter("send") != null )
         //Send specific processing.

Views: 12852 | Added by: The_Kat | Rating: 5.0/1
Total comments: 3
0 Spam
3 The_Kat  
Thanks to both of you. Very nice to know I've helped.

1 Spam
2 Abd  
thanks man you are a life saver
i haven't tested it yet but it seems a 100% perfect solution :)

1 Spam
1 Vtanathip  
Thank you,for your code example.

Name *:
Email *:
Code *:
Login form
«  February 2011  »
Entries archive
Site friends
  • Create your own site
  • My Blog
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    Copyright MyCorp © 2019
    Website builderuCoz