Spring MVC: Form Management

JC
2004-06-13
2004-08-04
  • JC

    JC - 2004-06-13

    Can some Spring guru find some time to give us a detailed exposition on Spring's Form Management Technology?  Like Spring's Configuration Technology, Form Management is a very critical aspect to the power of the framework.  It needs some considered (not half-measured) discussion.

    When a handler or other method in a Controller returns a ModelAndView object with a viewName and either a model object or a keyName and valueObject, what are the processes that follow, leading to the actual display of the view?

    If a form has been displayed to a user in order to receive field values for an object that is to be saved to, (or updated in) a database, for instance, and the user fills in and submits the form, what sequence of events follows?  what should the programmer have been mindful of prior to displaying the form, and after the form has been submitted?

    I think the ModelAndView technology in Spring needs some better exposition.

    Thanks, any gurus?

     
    • Anonymous - 2004-08-02

      I agree with this poster.  I'm just starting up a new project on my own and I've decided to use spring.  I've invested about a week's worth of evening time trying to learn the bastard and so far I must say that I am not impressed.  There just seems to be too much complexity for my taste, but I will soldier on.  I'm trying now to wire up the front to the back and find myself digging through the examples in order to figure out how to make this part work.  An outline of the mechanics of the form submission/url navigating would be great.
      As long as I'm writing this, does anyone have an  answer for how I would handle URLs, submitted without a form?  I'm guessing I won't have access to the onSubmit method, but I hope there is an easy way to get access to the values sent in through the request. 
      Thanks,
      Alex

       
    • Anonymous - 2004-08-03

      I dont know if i understand your questions clearly but i'll try my best. Usually when you submit a url that does not contain a form it should be handled by a controller that implements either Controller or MultiActionController interfaces. You can have a look at the methods on those interfaces.
      If on other hand you send url request a for a page that has a form the url should be mapped to  a subclass of a SimpleFormController (or other AbstractFormControllers depending on your requirements). But simpleformcontroller usually suffice. You  need to map this url to a formcontroller bcos the formcontroller would need to work on the form b4 it's displayed. The same formcontroller would also handle the submission using the onSubmit() method.
      Have a look at AbstractFormController and SimpleFormController in the API to see how forms are really handled.
      The main thing basically is that FormControllers need to do some magic before rendering a form (GET) and then also Handle the submission (POST) of the same (now populated) form.

      HTH

       
    • Anonymous - 2004-08-03

      To joshua6,
      I'll also try my luck here, ModelAndView is just at it says: After processing in the controller you would need to return a response which is basically a page(html,xslt,jsp,velocity,e.t.c). To allow view technology susbstitution spring uses logical view names and then delegates the resolution of the mapping between the view and the actual page to display to ViewResolvers(examples include InternalViewResolver and ResourceBundleViewResolver). You can think of ViewResolvers to be maintaning a mapping between the logical viewname(the one that you return with ModelAndView when your controller returns) and the actual resource(page) for results and display. The Model is the data that you want to display on your page and i think it's stuffed in the request object and thereby you can access it in the page by using its key that you specified when returned the ModelAndView.

      Regarding Viewresolvers, have a look at how the InternalViewResolver works by looking at the MVC step-by-step tutorial and ResourceBundleViewResolver look at petclinic.As its name says ResourceBundle~ it therefore uses a property file (resource bundle) called views.properties to maintain its mappings. Look under /WEB-INF/classes/ on petclinic. Remeber that for MVC the most important file is *-servlet.xml file which is heavily used by the dispatcher servlet that you define in web.xml.

      About form management you can look at my above post.

      HTH

       
    • Anonymous - 2004-08-04

      Thanks HTH.

      So if I want to display a page which has a form, even though the request did not come from a form, I have to use a FormController? 

      Right now I am (trying to) using an implementation of Controller to return some hardcoded data via the handleRequest method.  It worked ok before I tried hooking up the form which I was going to use to try and modify the data on the page, so your explanation would explain my current difficulties. 

      Right now no matter what I try I get  a
      "Could not find Errors instance for bean X in request: add the Errors model to your ModelAndView via errors.getModel() " error inside tomcat. 

      I will try and change the Controller which is rendering the page to an extension of SimpleFormController.  One more question in this regard though - when I've tried this in the past I've gotten a compilation error warning me that handleRequest was final and could not be ovverriden.  Given that I just want to use this controller to pass hardcoded data to the screen, what's the best way to go about it?  Is the formBackingObject method the one which needs to return the data? 

      Thanks again,
      Alex

      p.s. I guess this post should be in the help section..

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks