Announcement Announcement Module
No announcement yet.
Using Web Form with Multiple Beans Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Web Form with Multiple Beans


    I am relatively new to Spring and would really appreciate some help/advice as to how to implement the following problem.

    I have a 'User' who is logged in and authenticated. Associated with the 'User' account are either 1 or 2 'User Profiles' representing whether the account is for a single person or a couple.

    On the edit profile page the controller requests the 'User' account bean which eagerly fetches and stores as a Set the account profile beans and then the 'User' bean is added to the Model so that the View may access it.

    My question is around what is the best way to manage form submissions of data where you have more than 1 child object/bean attached to your main backing bean?

    For example, if the 'User Profile' bean contains fields 'gender, age, hair colour' how should that go into the form because for the beans to work correctly don't the fields in the form have to exactly match those defined in the bean? i.e. on an account where the type is couple and there are 2 profiles, how do I put 2 sets of fields with exactly the same names into the form, or more to the point, how does Spring know which choice on the front end has been made for which of the 2 profiles?

    Should I be creating 2 completely seperate and uniquely named backing beans specifically for this issue? I was hoping to avoid this if possible, but am stuck getting to grips with what best practice is in these kind of situations.


  • #2

    I recommend a book - - Expert Spring MVC and Spring WebFlow. Although it covers Spring 2, but the ideas, and especially form binding are general.

    Shortly: if you have a "client" bean which is a Client.class object, which have two UserProfiles in list:
    class Client {
       private List<UserProfile> profiles = ...;
    then your JSP can have such form fields:
    <form:form command="client">
       <form:input path="profiles[0].name" />
       <form:input path="profiles[1].name" />
    So - use "[]" notation which handles tables, lists and maps.

    Grzegorz Grzybek


    • #3
      I am new and want to study spring. When I can get a good book which teach me how to use.


      • #4
        Hi Grzegorz,

        Your answer was perfect, thank you very much for your help

        Kind Regards,


        • #5
          Hi Grzegorz,
          Could I bother you to show me how you tell the jsp to use the "client" form ? Do you do something like create a new Client object and put it into a ModelMap object?
          Thanks !


          • #6

            The best place to see how to create Spring MVC application is PetClinic sample. Here is sample controller:

            And here's how object created in controller is passed to view:
            public class AddVisitForm {
               private final Clinic clinic;
               @RequestMapping(method = RequestMethod.GET)
               public String setupForm(@PathVariable("petId") int petId, Model model) {
                  Pet pet =;
                  Visit visit = new Visit();
                  model.addAttribute("visit", visit);
                  return "pets/visitForm";
            Look at the bold text. The model is just a map, which is then (after your controller method returns) put into the request as request attributes which are then available in JSPs. In this example the <form:form /> tag should use command="visit" attribute.

            I recommend reading the PetClinic sample - it's a collection of best practices.

            Just do:
            svn co petclinic
            Grzegorz Grzybek