在 Hibernate 验证器下在 Spring 中验证表单的问题

发布于 2024-12-01 01:35:04 字数 1788 浏览 0 评论 0原文

我在 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]

我将非常感谢任何帮助

i have a problem with a simple form in spring that use validating with hibenate validator
my form:

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

my controller for 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";
}

my problem is when my form is invalide that throw exception!!i want jsp view of errors

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]

i will be realy greatfull for anyhelp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2024-12-08 01:35:04

来自 文档

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

看起来像你的情况..

From documentation:

The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more that one model object and Spring will create a separate BindingResult instance for each of them [...] To get this working you have to reorder the parameters [...]

Looks like your case..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文