我有一个控制器,其中有一个GET方法显示一个表单POJO,该POJO将被相应的POST捕获。但是,在执行POST请求时,将创建表单POJO,但从不填充,并且不会显示任何错误(除了空值的验证错误)。
我使用的是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");
}
}我的视图非常简单,使用spring表单来创建表单:
<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>而表单本身..。
@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...
}提前感谢!
发布于 2010-11-05 17:57:03
嗯,我刚刚开始使用Spring,但是我确实有这个场景,所以我想我可以告诉你我做了什么不同的事情。注释内联在下面的代码中
@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"https://stackoverflow.com/questions/4103320
复制相似问题