im new to spring mvc and ive been trying to bind a nested object in spring without results using "modelAttribute".
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)
public class car{
private int idCar;
private String brand;
private String color;
private Owner owner; //nested object
//getters and setters
}
then i have a get and a post controller.. in the get controller i do something like this:
@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;
}
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):
@RequestMapping(value="/showCar/process", method=RequestMethod.POST)
public ModelAndView addCar(@ModelAttribute("car") Car car, BindingResult result) {
car.getOwner(); //error, null.
}
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.
so if someone can answer to this with a little example it would be very useful.
thanks.