Hi all,
I'm new in Spring so sorry if I don't use the right vocabulary.
I've some trouble submitting a form with spring MVC when the object representing the form have a complex attribute.
Easier to explain with some code, I've something like this :
House is a class that I defined with several attributes. I have a list of houses pre-defined, and I would like that the user choose among them in the form.Code:public class Person { private String name; private int age; private House house; //+getters and setters }
So here is a part of my controller :
Then I arrive to my .jsp :Code:@RenderMapping public ModelAndView showForm(RenderRequest request, RenderResponse response){ ModelAndView mav= new ModelAndView("form"); ArrayList<House> houses = service.getHouses(); mav.addObject("houses", houses); //Command object Person person = new Person(); mav.addObject("person", person); return mav; }
Then I have my handler to process the form :Code:<form:form name="form" method="post" action="${url}" commandName="person"> <p> <form:select id="choosehouse" path="house"> <form:option value="" selected="selected">Select a house</form:option> <form:options items="${houses}" itemValue="id" itemLabel="name"/> </form:select> </p> <p>Name :<form:input path="name"/></p> <p>Age :<form:input path="age"/></p> <input type="submit"/> </form:form>
the problem is that if I put this, it does not work because the form will only throw the id of the house and not the corresponding house entity so I raise an exception.Code:@ActionMapping public void onSubmit(@ModelAttribute Person person)
So I need a way either to throw the entire house entity instead of just the id (not sure that it is possible) or to complete my person entity before it goes to my onSubmit.
Thanks for your replies!


Reply With Quote
