title: Spring bind nested object
body: <p>im new to spring mvc and ive been trying to bind a nested object in spring without results using "modelAttribute".</p>
  
  <p>for example: i have this class  (this is a hard coded example so dont expect a perfect syntax it is just for you to understand the issue)</p>
  
  <pre><code>public class car{
  
  private int idCar;
  private String brand;
  private String color;
  private Owner owner; //nested object
  
  //getters and setters
  
  }
  </code></pre>
  
  <p>then i have a get and a post controller.. in the get controller  i do something like this:</p>
  
  <pre><code>   @RequestMapping(value="/showCar", method = RequestMethod.GET)
      @ModelAttribute("car")
      public ModelAndView showCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView view = new ModelAndView("showCar");
        Car car=new Car();
        Owner owner=OwnerService.findOwnerById(1) //an owner that exists in the database
        car.setOwner(owner); //setting Owner object to car object
        view.addObject("car", car); // adding the car to the model
        return view;
      }
  </code></pre>
  
  <p>post controller (after i make a submit on my jsp page i expect to receive in my post controller the car object with the nested object):</p>
  
  <pre><code> @RequestMapping(value="/showCar/process", method=RequestMethod.POST)
      public ModelAndView addCar(@ModelAttribute("car") Car car, BindingResult result) {
  
  
        car.getOwner();  //error, null.
  
  
  
          }
  </code></pre>
  
  <p>i haven't been able to make this work.. what is the proper way to bind a nested object to my controller? this is  very basic stuff and i think that any person that have been using spring for a while must know, but for some reason i have not found any example of nested objects binding to controllers.</p>
  
  <p>so if someone can answer to this with a little example it would be very useful.</p>
  
  <p>thanks.</p>