Results 1 to 3 of 3

Thread: Need to Populate Form data into multiple java beans

  1. #1
    Join Date
    Aug 2008
    Location
    Bangalore
    Posts
    11

    Default Need to Populate Form data into multiple java beans

    Dear all,
    This is my first post.I've a problem in populating form data into java beans using spring MVC layer.
    I need to populate the form data into multiple java bean objects.Is this possible in Spring MVC ? (can we specify multiple commandNames and classes for a form controller). I knew SimpleFormController which will allow me to set all my form fields into single java bean object.
    If anybody has any ideas please let me know.

    Thanks,
    Sekhar.

  2. #2

    Default

    You should be able to populate to more than one bean. Simply specify all the beans you want to populate as fields in your form backing object. Then, you can bind to them. It would be something like this:

    Code:
    public class MyFormBackingObject{
      private final MyBean1 beanOne = new MyBean1();
      private final MyBean2 beanTwo = new MyBean2();
      //getter methods would follow
    }
    then, in your SimpleFormController

    Code:
    ...
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
      return new MyFormBackingObject();
    }
    
    ...
    
    protected ModelAndView onSubmit(
    			HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
    			throws Exception {
      MyFormBackingObject myForm = (MyFormBackingObject) command;
      MyBean1 beanOne = myForm.getBeanOne();
      MyBean2 beanTwo = myForm.getBeanTwo();
      //now do anything you need to do with them.
    }

    Just in case you aren't aware of it, you may wish to populate these beans over more than one page (page 1 sets first bean, page 2 sets second bean), using org.springframework.web.servlet.mvc.AbstractWizard FormController. You can easily populate multiple beans from a SimpleFormController, but if there is a lot of data in the beans, it might make sense to space it out over more than one page.

  3. #3
    Join Date
    Aug 2008
    Location
    Bangalore
    Posts
    11

    Default

    Thanks a lot uaine. It worked for me.

Posting Permissions

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