Announcement Announcement Module
No announcement yet.
Processing form using a Controller w/ multiple submit button Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Processing form using a Controller w/ multiple submit button

    I am new to Spring and can't figure out how to handle a submit with multiple submit buttons using one of Springs Controllers. The success view depends on which button the user clicks. For example, if a user clicks the add button they go to the add view and if the user clicks the delete button then they go to the delete view. Since the SimpleFormController is only allow 1 success view then I am unable to use this straight out of the box. I thought about adding custom properties to the controller bean definition defining the add, edit, and delete views and then ignoring the success view but I wasn't sure if this was the right way to implement this functionality. Could someone please suggest some ways to handle this functionality?

    Thanks in advanced!!

  • #2
    You can either use the MultiActionController or map the different actions to unqiue URLs (which you will end up doing anyway). There is then no need to go via any controller, the "add" button can simply be a link to "add.html" etc.


    • #3
      I beleive as suggested the MultiActionController is designed for what you want to do.
      However I've never used it: I've gone the route of a SimpleFormContoller with, as you suggest, custom properties for other views, and testing the button clicked in the controller to control the processing and the view returned.
      I think that's a perfectly valid approach.



      • #4
        Spring 2.5 annotation:

        if the form has these buttons specified:

        <input type="submit" class="button" name="save" value="Save" />
        <input type="submit" class="button" name="delete" value="Delete" />
        <input type="submit" class="button" name="cancel" value="Cancel" />

        you may direct to different url request according to button pressed with one controller.

        for cancel button,

        @RequestMapping(params = "cancel", method = RequestMethod.POST)
        public String cancelUpdateUser(HttpServletRequest request) {
        return "redirect:/users.html";

        what request mapping does is to scan post request if it contains params name = cancel.

        for save button,

        @RequestMapping(params = "save", method = RequestMethod.POST)
        public String saveUser(HttpServletRequest request, @ModelAttribute User user, BindingResult result, SessionStatus status) {
        // validate your result
        // if no errors, save it and redirect to successView.


        • #5
          thank you for this post. it seems so basic it should be in the main spring documentation. i struggled with this for hours and this is the only post i have seen anywhere that has any sanity to it.


          • #6
            @ rhtran

            Thank you for this post!
            Last edited by pietromarrone; May 5th, 2010, 08:52 AM.


            • #7

              but what should be the form action="????" Because irrespective of what name you give to the submit button the browser will always redirect you to the method which you gave in the form action. which is a property for multiaction controller.


              • #8
                There is then no need to go via any controller, the "add" button can simply be a link to "add.html" etc. Attachment
                Attached Files


                • #9
                  First of all, thank you very much for this post.
                  I have tried use the MultiActionController with a form that has two buttons ("next" and "previous").
                  Actually the program process’s is detailed below:
                  - I access the form page
                  - The "MultiActionController" take the form responsibility.
                  - I wrote @ RequestMapping (params = "whatever", method = RequestMethod.POST) then the Controller tells me that I have not used any buttons. (It’s true because I have not accessed to Form)
                  - The fact is that I do not know what I should to do for the "Controller" simply show the view and then take control.
                  - When I use a SimpleFormController I first access to ReferenceData and when I press the submit button then the Controller take the control. But MultiActionController does not do it.
                  Anyone have a complete example that can be shared