The Kat's Work - Blog
Main | Blog | Registration | Login
Thursday
2024-03-28
9:13 AM
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: 13129 | Added by: The_Kat | Rating: 5.0/1
Total comments: 3
3 The_Kat  
0
Thanks to both of you. Very nice to know I've helped.

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

1 Vtanathip  
1
Thank you,for your code example.

Name *:
Email *:
Code *:
Login form
Adverts
Search
Calendar
«  February 2011  »
SuMoTuWeThFrSa
  12345
6789101112
13141516171819
20212223242526
2728
Entries archive
Site friends
  • Create your own site
  • Spree4.com
  • My Blog
  • Statistics

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