In many cases, this should be sufficient if the POST parameter names match the POJO attribute names. The correct way is to use the Spring taglib form and bind it to your pojo:
@Controller @RequestMapping("/user") class UserController { ... @RequestMapping(value="/login", method=RequestMethod.GET) public ModelAndView get() { return new ModelAndView().addObject("formBackingObject", new User()); } @RequestMapping(value="/login", method=RequestMethod.POST) public String save(User user) {
And then in your JSP:
// eg in user/login.jsp <form:form method="post" commandName="formBackingObject" action="/user/login.html"> <form:label path="username"><spring:message code="label.username" /></form:label> <form:input path="username" cssErrorClass="error" /> <form:label path="password"><spring:message code="label.password" /></form:label> <form:password path="password" cssErrorClass="error" /> <p><input class="button" type="submit" value="<spring:message code="label.login" />"/></p> </form:form>
You can nest your attributes (e.g. address.street if the user has an address property), I don't think Spring will accept more than one command object.
Daff source share