@ModelAttribute参数未填充

时间:2010-11-05 03:11:36

标签: spring spring-mvc annotations

我有一个控制器,其中我有一个GET方法显示一个表单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");
 }
}

我的观点很简单,使用弹簧形式创建表单:

<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...
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

我刚刚开始搞乱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"); } }

我的视图中的表单标记的使用略有不同

form:form cssClass =“registerForm”commandName =“registerForm”