hasError 不起作用;使用 Hibernate 进行 Spring 3 MVC 验证
hasError 不起作用;使用 Hibernate 进行 Spring 3 MVC 验证!
也许我做得不对,但有人可以告诉我我的 hasErrors() 是错误的吗?我把错误放在我的表单中,但 hasErrors 从未尝试过?
控制者:
@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(名称=“文章”) 公开课文章{
@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;
}
}
hasError is not working; Spring 3 MVC Validation with Hibernate!
Maybe I am not doing this right but can someone tell me what my hasErrors() is false. I am puttting errors is my form but the hasErrors is never try?
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";
}
Form:
@Entity
@Table(name = "article")
public class 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 JSR-303 验证设置正确(您有 hibernate-validator jar 和
),那么您应该只使用@Valid @ModelAttribute(.. ) ..
If JSR-303 validation is setup properly (you have hibernate-validator jar, and
<mvc:annoatation-driven />
) then you should just use@Valid @ModelAttribute(..) ..