hasError 不起作用;使用 Hibernate 进行 Spring 3 MVC 验证

发布于 2024-10-25 18:08:36 字数 1718 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-11-01 18:08:36

如果 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(..) ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文