@Size 和 @Pattern 注释在 Spring MVC 中不起作用

Him*_*olu 2 spring spring-mvc

将以下 jar 添加到我的 WEB-INF 中的 lib 文件夹中:

  • 同学-1.0.0
  • javax.el-2.2.4
  • javax.el-api-2.2.4
  • jboss-logging-3.1.3.GA
  • 验证-api-1.1.0.Final

我添加的代码片段是:

从模型对象:

public class UserDetails {

    @Pattern(regexp="(^0-9}*") 
    private String userName;   

    @Size(min=2,max=10)
    private String firstName;    
    private String lastName;
    private String emailId;
    private ArrayList<String> accountType;
    private ArrayList<String> gender;

    @Size(min=2,max=10)
    private Long accountNo;
Run Code Online (Sandbox Code Playgroud)

FromController 类:

@RequestMapping(value = "/UserAccount.html", method = RequestMethod.POST)
public ModelAndView userAccountForm(
        @Valid @ModelAttribute("user") UserDetails user,
        BindingResult result) {

    if (result.hasErrors()) {
        ModelAndView model1 = new ModelAndView("LoginForm");
        return model1;
    }

    ModelAndView model1 = new ModelAndView("UserAccount");
    return model1;
}
Run Code Online (Sandbox Code Playgroud)

我的调度程序 servlet 有

<mvc:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)

如果我错过了任何基本的东西,请告诉我。

Him*_*olu 5

我通过将以下 jar 文件添加到我的库中来解决这个问题。

hibernate-validator-5.1.3.Final

在我通过教程学习时,虽然提到了这个 jar,但我错过了添加。

感谢大家的意见。