Results 1 to 5 of 5

Thread: How to submit a form back to its controller?

  1. #1
    Join Date
    Oct 2004
    Location
    NYC, USA
    Posts
    13

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    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
    Chris Harris
    Carlisle, UK

  3. #3
    Join Date
    Oct 2004
    Location
    London, UK
    Posts
    71

    Default

    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 at 10:57 AM.

  4. #4
    Join Date
    Oct 2004
    Location
    NYC, USA
    Posts
    13

    Default

    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

  5. #5

    Default

    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"

Similar Threads

  1. Replies: 8
    Last Post: Oct 30th, 2012, 11:33 AM
  2. Replies: 6
    Last Post: Jul 20th, 2007, 05:56 AM
  3. Replies: 0
    Last Post: Jun 10th, 2005, 08:22 AM
  4. Replies: 9
    Last Post: Aug 25th, 2004, 10:57 PM
  5. Controller to Form Navigation?
    By cnelson in forum Web
    Replies: 1
    Last Post: Aug 20th, 2004, 06:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •