Spring 3验证方法未验证

发布于 2024-11-17 05:24:11 字数 1874 浏览 1 评论 0原文

我在寻找如何在 Spring 3 中进行验证时遇到了一些问题。 到目前为止我所拥有的是这个 Java Bean....

public class User {

@NotEmpty
@Size(min=2, max=40)
private String name="";

@Email
private String email="";

public String getName(){
    return name;
}

public String getEmail(){
    return email;
}

public void setName(String name){

    this.name=name;
}

public void setEmail(String email){

    this.email=email;
}

}

这是链接到包含表单的页面的方法:

    @RequestMapping(value="/form", method=RequestMethod.GET)
public String form(Model model){


    model.addAttribute("user",new User());
    return "form";
}

这是我的控制器中的方法,它“应该”验证

    @RequestMapping(value="/displayUser",method=RequestMethod.POST)
public String displayUser(Model model,@Valid User user,BindingResult result){


    if(result.hasErrors()){
       return "form";   
    }
    userList.add(user);
    model.addAttribute("user",user);

    return "displayUser";
}

@Validate 注释应该进行验证,但就我而言,事实并非如此。

你看到我的代码有什么问题吗?

顺便说一句,这是我的形式“一切都非常简单,这只是为了测试目的

   <f:form method="post" action="displayUser" commandName="user">
 <table align="center" border="1">
  <tr>
  <td>Your details</td>
  </tr>
  <tr>
  <td><f:label path="name">Name:</f:label></td><td><f:input path="name" /></td><f:errors path="name"></f:errors>
  </tr>
  <tr>
   <td><f:label path="email">Email:</f:label></td><td><f:input path="email" /></td><f:errors path="name"></f:errors>
  </tr> 
  <tr>
  <td><input type="submit" value="register" /></td>
  </tr>
 </table>
</f:form>

有什么建议吗?(我使用java 5,并且 javax.validation.Validator 不可用)

I'm having some issues finding out how to validate in spring 3.
What i have so far is this Java Bean....

public class User {

@NotEmpty
@Size(min=2, max=40)
private String name="";

@Email
private String email="";

public String getName(){
    return name;
}

public String getEmail(){
    return email;
}

public void setName(String name){

    this.name=name;
}

public void setEmail(String email){

    this.email=email;
}

}

This is the method that links to the page containing the form:

    @RequestMapping(value="/form", method=RequestMethod.GET)
public String form(Model model){


    model.addAttribute("user",new User());
    return "form";
}

And this is the method in my controller, which "should" validate

    @RequestMapping(value="/displayUser",method=RequestMethod.POST)
public String displayUser(Model model,@Valid User user,BindingResult result){


    if(result.hasErrors()){
       return "form";   
    }
    userList.add(user);
    model.addAttribute("user",user);

    return "displayUser";
}

The @Validate annotation should make the validation, but in my case it does not.

Do you see something wrong in my code?

by the way this is the form i have "all is very simple, this is just for test purposes

   <f:form method="post" action="displayUser" commandName="user">
 <table align="center" border="1">
  <tr>
  <td>Your details</td>
  </tr>
  <tr>
  <td><f:label path="name">Name:</f:label></td><td><f:input path="name" /></td><f:errors path="name"></f:errors>
  </tr>
  <tr>
   <td><f:label path="email">Email:</f:label></td><td><f:input path="email" /></td><f:errors path="name"></f:errors>
  </tr> 
  <tr>
  <td><input type="submit" value="register" /></td>
  </tr>
 </table>
</f:form>

Any advise? (i use java 5 and there the javax.validation.Validator isn't available)

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

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

发布评论

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

评论(1

如梦亦如幻 2024-11-24 05:24:11

确保添加javax.validation的validation-api和hibernate验证器。如果您使用 Maven,请在您的依赖项上添加以下内容:

           <dependency>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                    <version>1.0.0.GA</version>
            </dependency>


             <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-validator</artifactId>
                    <version>4.0.2.GA</version>
            </dependency>

在您的 applicationContext 上启用 Spring 注释驱动

<mvc:annotation-driven />

Make sure to add javax.validation's validation-api and hibernate validator. If you use maven add the following on your dependencies:

           <dependency>
                    <groupId>javax.validation</groupId>
                    <artifactId>validation-api</artifactId>
                    <version>1.0.0.GA</version>
            </dependency>


             <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-validator</artifactId>
                    <version>4.0.2.GA</version>
            </dependency>

Enable Spring annotation driven on your applicationContext

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