I have a controller in which I have a GET method display a form POJO, which is to be captured by a corresponding POST. However, when the POST request is executed, the form POJO is created but never populated, and no errors are shown (besides validation errors for the nulls).
I'm using Spring 3.0.
@Controller
public class UserController {
@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView renderRegisterForm(
@ModelAttribute("registerForm") UserRegisterForm registerForm) {
ModelAndView mav = new ModelAndView("user.register");
mav.addObject("registerForm", registerForm);
return mav;
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerForm(
HttpServletRequest request,
@Valid @ModelAttribute("registerForm") UserRegisterForm registerForm,
BindingResult results) {
// All fields in registerForm are null, results has errors for the @NotNull annotations
return new ModelAndView("user.register");
}
}
My view is preety simple, using spring forms to create the form:
<form:form cssClass="registerForm" modelAttribute="registerForm" method="post" action="/register">
<div class="inputContainer">
<form:label path="name">
<spring:message code="user.edit.name"/>
</form:label>
<form:input path="name"/>
<form:errors path="name" cssClass="error"></form:errors>
</div>
<div class="inputContainer">
<form:label path="email">
<spring:message code="user.edit.email"/>
</form:label>
<form:input path="email"/>
<form:errors path="email" cssClass="error"></form:errors>
</div>
<div class="inputContainer">
<form:label path="password">
<spring:message code="user.edit.password"/>
</form:label>
<form:password path="password"/>
<form:errors path="password" cssClass="error"></form:errors>
</div>
<div class="inputContainer">
<form:label path="repeatPassword">
<spring:message code="user.edit.repeatPassword"/>
</form:label>
<form:password path="repeatPassword"/>
<form:errors path="repeatPassword" cssClass="error"></form:errors>
</div>
<div class="submit-button">
<input type="submit" value="<spring:message code="register"/>"/>
</div>
</form:form>
and the form itself...
@FieldMatchList({@FieldMatch(first="password", second="repeatPassword")})
public class UserRegisterForm {开发者_开发问答
@NotNull
@Size(min = 1, max = 50)
private String name;
@NotNull
@Email
@Size(max=100)
private String email;
@NotNull
@Size(min=6, max=32)
private String password;
@NotNull
@Size(min=6, max=32)
private String repeatPassword;
// Getters and setters...
}
Thanks in advance!
Well I've just started messing around with Spring, but I do have this scenario working, so I guess I can tell you what I have done differently. Comments are inline in the code below
@Controller
public class UserController {
@RequestMapping(value = "/register", method = RequestMethod.GET)
public ModelAndView renderRegisterForm() {
// Do not use the method parameters, instead instantiate a new binding object yourself
UserRegistrationForm registerForm = new UserRegistrationForm();
ModelAndView mav = new ModelAndView("user.register");
// Use mav.getModel().put() instead
mav.getModel().put("registerForm", registerForm);
return mav;
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public ModelAndView registerForm(
HttpServletRequest request,
// I didn't need to annotate my bound object with @ModelAttribute when using @Valid
@Valid UserRegisterForm registerForm,
BindingResult results) {
// All fields in registerForm are null, results has errors
// for the @NotNull annotations
return new ModelAndView("user.register");
}
}
I also have a slightly difference usage of the form tag in my view
form:form cssClass="registerForm" commandName="registerForm"
精彩评论