Announcement Announcement Module
Collapse
No announcement yet.
Need to Populate Form data into multiple java beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Thanks a lot uaine. It worked for me.

      Comment

      Working...
      X