Announcement Announcement Module
Collapse
No announcement yet.
How to submit a form back to its controller? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to submit a form back to its controller?

    Hi all,

    I'm trying to code a small form which enables the user to add/delete an object to a list, or alternately, save all of the objects on the page. Importantly, I'm trying to accomplish all of this with one form and one controller since it seems to be a simple repetitive operation in which only the number of objects displayed varies in the end...

    I've successfully coded the 'save' part of the operation, but adding and deleting of the individual objects (i.e. text input boxes of a form) are causing me a major headache.

    Thus far:

    I extended SimpleFormController and overrode the following methods:
    Code:
    public class BlockTradeEditController extends SimpleFormController{
    
       void initBinder(HttpServletRequest request, ServletRequestDataBinder binder){
       }
    
       Object formBackingObject(HttpServletRequest request){
       }
    
       ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors){
       }
    
       ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors){	
    	String view;
    		
    		if(request.getParameter("add") != null){
                            //code to add another row/object here...
    			view = getFormView();	//name of form (this is likely my problem?)		
    		}
    		else{
                            //plain save object + get success view code here...
    			view = getSuccessView();  //success view i.e. a .jsp with a list, standard fare....
    		}
    		
    		return new ModelAndView(view, map);
    	}
    Essentially I'm having trouble with submitting the form with the "add" parameter, and having the controller call all of it's setup methods again (i.e. showForm, formBackingObject, initBinder, etc.) since they are necessary for proper dislplay of the form...

    I know this is likely a noobish mistake, but I'm just starting out in Spring and am stuck at this point. Any help would be greatly appreciated... And don't be shy about suggesting other solutions.

  • #2
    I don't think there's anything wrong with your approach, I certainly use the same basic patterm.

    Without knowing what your view needs, and what you're putting in your model, it's hard to know just where the problem lies. You need to make sure you put in your model everything your view is going to need.
    For example, to make sure your model has the errors instance and the form backing object that was populated from the request, you can initialize your model with
    Code:
    Map map = errors.getModel();
    Then add anything else to the map that your view will need.

    If you've done some work on the command object, or created a new one which nees to be returned as the form backing object, you can do
    Code:
    Map map = (new BindException(yourCommandObject, getCommandName())).getModel();
    Hope that helps

    Comment


    • #3
      if the form isnt a sessionForm = true then it acts like a new request and it will do the formBackingObject first before processing the formSubmission
      but it does have to a submit called.

      if you look at
      http://forum.springframework.org/showthread.php?t=11086
      it is a similar problem

      if you are using different submit buttons you may want to try
      Code:
      if (WebUtils.hasSubmitParameter(request, "add")) {
      				//code to add another row/object here...
               view = getFormView();   //name of form (this is likely my problem?)  
      			}
      if (WebUtils.hasSubmitParameter(request, "_finish")) {
      				//plain save object + get success view code here...
               view = getSuccessView();  //success view i.e. a .jsp with a list, standard fare....   
      			}
      but you will need to have a sessionForm for this to work i think.
      Last edited by robyn; May 14th, 2006, 10:57 AM.

      Comment


      • #4
        Thanks for your help.

        I got rid of the sessionForm attribute, allowing the controller to call formBackingObject each time the Submit button is pressed... this essentially solved most of my problems.

        Thanks again,
        Petet

        Comment


        • #5
          How to handle this when using Spring 3 and @RequestMapping and @RequestParam? Is there a way to get your hands on a request object?

          I'm trying something like
          Code:
          @RequestMapping(method = RequestMethod.GET)
              public String myMethod(Model model, @RequestParam("id") int id) {
          
          if (WebUtils.hasSubmitParameter(should be the request , "button_id")) {
              System.out.print("Jee");
          }
          
          return "viewName"

          Comment

          Working...
          X