【问题标题】:Spring MVC 3 and validation errorSpring MVC 3 和验证错误
【发布时间】:2012-06-02 09:16:06
【问题描述】:

我正在尝试使用基于 Spring MVC 的 javax 创建简单的验证。我启用了注释驱动。我想在表单中显示错误,但出现 500 页错误:

500 错误页面

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'profile' on field 'email': rejected value [xx@ss]; codes [Pattern.profile.email,Pattern.email,Pattern.java.lang.String,Pattern]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [profile.email,email]; arguments []; default message [email],[Ljavax.validation.constraints.Pattern$Flag;@718df055,^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$]; default message [Nieprawidłowy e-mail]
Field error in object 'profile' on field 'login': rejected value [xx]; codes [Size.profile.login,Size.login,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [profile.login,login]; arguments []; default message [login],20,4]; default message [Login musi min. długość 4 znaków, max. 20 znaków]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

控制器

@RequestMapping(value="/register", method=RequestMethod.POST)
    public String register(
            @Valid Profile profile,
            @RequestParam("recaptcha_challenge_field") String challenge,
            @RequestParam("recaptcha_response_field") String response, 
            BindingResult result, Model model, HttpServletRequest request) {

        ReCaptchaResponse cResponse = captcha.checkAnswer(request.getRemoteAddr(), challenge, response);
        if(result.hasErrors() || !cResponse.isValid()) {
            String message = cResponse.getErrorMessage();
            String html = captcha.createRecaptchaHtml(message, null);
            model.addAttribute("profile", profile);
            model.addAttribute(CAPTCHA_HTML, html);
            return "login/register";
        }
        return "ok";
    }

部分模型(仅验证此字段)

@NotBlank
    @Size(min=4, max=20)
    @Pattern(regexp="^([\\w]+)$")
    private String login;

    @NotBlank
    @Pattern(regexp=("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))
    private String email;

如何解决?

【问题讨论】:

    标签: java spring validation spring-mvc hibernate-validator


    【解决方案1】:

    也许尝试做 maven clean 并安装到您的本地 repo 中?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2011-07-21
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      相关资源
      最近更新 更多