1

我即将在Spring 3.1. 我正在使用注释来验证我的模型,如下所示:

模型:

@Column(name = "mailAddress", nullable = false)
@Email
private String mailAddress;

@Column(name = "school", nullable = false)
@NotBlank
@Size(min = 3, max = 100)
private String school;

控制器:

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addBooking(
            @ModelAttribute("new-booking") @Valid Booking booking,
            BindingResult result, Map<String, Object> model) {

        if (result.hasErrors()) {
            return "booking";
        }
            return "success";
     }

问题是,它验证了school但不是mailAddress. 如果您输入一个空的mailAddress,它将接受它。

4

2 回答 2

4

我认为您的验证问题可能与以下事实有关:使用@Email 验证注释,您的验证器也被认为是有效的电子邮件,其形式如下:“myname@service”,不以扩展名结尾(例如) .com

如果这是您的问题...这是正常的,因为它还考虑了您有一封具有以下格式的电子邮件的情况:yourname@localhost(内部电子邮件地址)

If you would validate a classic e-mail (having format: myname@someservice.com) I think that you have to write your custom validator.

I hope that I have understand your problem

于 2013-02-07T18:43:13.567 回答
2

I found the Issue. The Email Validator will accept Blank Emails. To fix this you only need to add a @NotBlank to it. Actually I thought the nullable = false would be enought, but wasn't.

@Column(name = "mailAddress", nullable = false)
@Email
@NotBlank
private String mailAddress;
于 2013-02-08T09:35:40.283 回答