Here was the answer that I was looking for, I did not understand that Spring, by default, will take all the parameters from the form view (such as "firstname" and "lastname") and can create an object for you by calling the setter methods of these parameters.
Controller:
@Controller public class MyFormProcessor { @RequestMapping("/formsubmit") public String handleForm(@Valid Person person, BindingResult errors, Map<String,Object> model){
Spring essentially does the following magic before handleForm for this request (obviously, in a more extensible way than I'm picturing for this simple example):
Person person = new Person(); person.setFirstname( request.getParameter("firstname") ); person.setLastname( request.getParameter("lastname") ); handleForm(person, anErrorsObject, new Model());
For verification, you can either create your own validator (which I don’t mention anything about), or if you include the Hibernate Validator in the classpath, then you can annotate the Person class (example below) and when you add the @Valid annotation, as I described in the example above, the Hibernate validator will check the class based on these annotations and send any errors to the error object (the BindingResult object is an extension of Springs Errors ), and for simple examples, the Errors object is an interesting component).
The grounded annotated Persons class for JSR-303 (for use with the @Valid option):
public class Person { @NotNull @Size(min=3, max=20) private String firstname; @NotNull @Size(min=3, max=20) private String lastname;
David parks
source share