在hibernate验证器下在spring中验证表单的问题

时间:2011-08-24 22:27:38

标签: hibernate spring-mvc hibernate-validator

我在Spring中使用带有hibenate验证器验证的简单表单有问题 我的表格:

 <form:form method="POST" commandName="info">

     username:<form:input path="username"/> <form:errors path="username"></form:errors>
     password:<form:password path="password"/><form:errors path="password"></form:errors> 
     <input type="submit" value="add new writer">
  </form:form>

我的表格控制器:

@RequestMapping(method=RequestMethod.POST,value={"/createnewuser"})
public String showCreateNewUserFrom( @javax.validation.Valid   Information info,Model model,BindingResult br)
{
    //System.out.println("hello post");
    if(br.hasErrors())
    {

        return "admin/createuserform";
    }
    else
    return "admin/createuserform";
}

我的问题是当我的表单无效时抛出异常!!我想要jsp查看错误

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'information' on field 'username': rejected value [vc]; codes [Size.information.username,Size.username,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [information.username,username]; arguments []; default message [username],10,4]; default message [username must be at least 4 and at most 10 character]
Field error in object 'information' on field 'password': rejected value [fg]; codes [Size.information.password,Size.password,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [information.password,password]; arguments []; default message [password],8,5]; default message [username must be at least 5 and at most 8 character]

我将非常感谢anyhelp

1 个答案:

答案 0 :(得分:0)

来自documentation

  

错误或BindingResult参数必须遵循立即绑定的模型对象,因为方法签名可能包含多个模型对象,而Spring将为每个模型对象创建一个单独的BindingResult实例[...]工作你必须重新排序参数[...]

看起来像你的情况..