I have a parent class and its child in my application model.
And have the following controller method:
@RequestMapping(method=RequestMethod.POST, value="/page")
public String postMethod(Model model, Parent obj, BindingResult result) {
// do something
}
but I want it to be able to handle instances of the child class, that was posted by form.
How can I do this?
You can create an object of required type as an implicit model attribute:
Since Spring doesn’t remember type of the object passed to the form automatically, you need to do it yourself, by adding a hidden form field whose value specifies type of the object (a
typefield in the example above).Another option is to avoid creating object from scratch by storing it in a session. It can be configured using
@SessionAttributesannotaton, see 15.3.2.9 Specifying attributes to store in a session with @SessionAttributes.