你好我下面一個例子,我發現Hibernate的驗證沒有驗證
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/
的問題是沒有錯誤在我的個人資料,我發現後。我應該。爲什麼會發生這種情況?
@Test
@Ignore
public void anotherTest() {
Profile profile = ProfileUtil.getProfile();
profile.setEmail("[email protected]");
profile.setSex("dafjsgkkdsfa");
BindingResult bindingResult = new BeanPropertyBindingResult(profile, "profile");
userController.postUser(new ModelMap(), profile, bindingResult);
if (bindingResult.hasErrors()) {
System.out.println("errors");
}
assertTrue(bindingResult.hasErrors());
profileService.deleteProfile(profile);
}
@RequestMapping(value = "/", method = RequestMethod.POST)
public View postUser(ModelMap data, @Valid Profile profile, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
System.out.println("No errors");
return dummyDataView;
}
data.put(DummyDataView.DATA_TO_SEND, "users/user-1.json");
profileService.save(profile);
return dummyDataView;
}
編輯: 這是配置文件。我現在正在測試性行爲,所以我認爲這很重要。
package no.tine.web.tinetips.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import no.tine.web.tinetips.util.CommonRegularExpressions;
import org.hibernate.validator.constraints.NotBlank;
@Entity
public class Profile {
@Id
@GeneratedValue
private Long id;
@NotNull(message = "profile.email.null")
@NotBlank(message = "profile.email.blank")
@Size(max = 60, message = "profile.email.maxlength")
@Pattern(regexp = CommonRegularExpressions.EMAIL, message = "profile.email.regex")
@Column(name = "Email", unique = true)
private String email;
@Pattern(regexp = "^[M|F]{1}$", message = "profile.sex.regex")
@Size(max = 1, message = "profile.sex.maxlength")
private String sex;
}
顯示您的個人檔案類源 – vacuum