hasError不起作用;使用Hibernate进行Spring 3 MVC验证!
也许我没有做到这一点,但有人可以告诉我,我的hasErrors()是错误的。我推测错误是我的形式但是hasErrors永远不会尝试?
Controaller:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String processValidatinForm(@ModelAttribute(" article") Article article,
BindingResult result) {
System.out.println("In");
if (result.hasErrors()) {
System.out.println("I found errors");
return "addArticle";
}
// Add the saved validationForm to the model
articleService.addArticle( article);
return "redirect:/articles.html";
}
形式: @实体 @Table(name =“article”) 公共类文章{
@Id
@GeneratedValue
@Column(name = "article_id")
private Long articleId;
@NotEmpty
@Size(max = 20)
@Column(name = "article_name", nullable = false, length=20)
private String articleName;
@NotEmpty
@Column(name = "article_desc", nullable = false)
private String articleDesc;
@Column(name = "date_added")
private Date addedDate;
public Article() {
}
public Long getArticleId() {
return articleId;
}
public void setArticleId(Long articleId) {
this.articleId = articleId;
}
public String getArticleName() {
return articleName;
}
public void setArticleName(String articleName) {
this.articleName = articleName;
}
public String getArticleDesc() {
return articleDesc;
}
public void setArticleDesc(String articleDesc) {
this.articleDesc = articleDesc;
}
public Date getAddedDate() {
return addedDate;
}
public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}
}
答案 0 :(得分:3)
如果正确设置了JSR-303验证(你有hibernate-validator jar和<mvc:annoatation-driven />
)那么你应该只使用@Valid @ModelAttribute(..) ..