In Spring 3, if I have a form object like so:
public Person {
String firstname = null;
String lastname = null;
// ... getter & setters ...
}
I now understand that I can receive this object as a parameter in my controller method and that Spring will bind the form parameters to the getters/setters of my Person object.
Now I want to use springs binding macros (equivalent to the JSP taglib’s for binding to form objects).
All the docs I read say that there is a default “command” object (what this means I don’t yet understand). It seems like I should be able to reference the Person object via a call such as:
#springBind("command.firstname")
But I don’t understand where “command” comes from or what it means, and I don’t understand what I need to do in my controller to make this “binding” possible.
- Do I need to add something to the model?
- Do I need to define for Spring that the Person object is the form object?
- Should I add <“command”, new Person()> to the model before rendering the page, or re-rendering the page after a form submission with errors?
- What went into the controller to make “command” meaningful to the macro?
I’m confused here and just missing some really really really simple concept I think.
Yes, you need to add the command object in the
Model. And you need to put it there already when rendering the empty form, somodel.addAttribute("command", new Person())is correct way. On validation errors you might want to use the submittedPersonobject instead, so that the user wouldn’t lose all his submission data.