Hibernate验证器不起作用

时间:2016-11-16 01:47:58

标签: java spring hibernate validation hibernate-validator

我正在使用Hibernate验证器与Hibernate和Spring,但似乎验证不起作用。当我没有输入字符串或输入1个字符的字符串(不在<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="terms" class="inputcheckbox" name="terms" type="checkbox"> <button onclick="validate()">Validate</button>require 'watir' switches = %W[--user-data-dir=c:\\chrome] # switches = %W[--user-data-dir=/chrome] => Linux or Mac prefs = { :download => { :prompt_for_download => false, :default_directory => "c:\\downloads" } } browser = Watir::Browser.new :chrome, :prefs => prefs, switches: switches browser.goto 'google.com' browser.text_field(title: 'Search').set 'Hello World!' browser.button(type: 'submit').click sleep 10 puts browser.title browser.close 之间)时没有显示任何错误,因此保存在表中。我错过了什么?

min=4

控制器:

max=20

在JSP中,我将此行显示为错误:

package dao;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name="etudiant")
public class Etudiant {

    @Id
    @Column(name="ID_ETUDIANT")
    @GeneratedValue
    private Long idEtudiant;

    @Column(name="NOM")
    @Size (min=4, max=20)
    private String nom;

    @Size(min=4, max=20, message="nom doit etre entre 4 et 20 svp..")
    @Column(name="PRENOM")
    private String prenom;

    @Column(name="DATE_NAISSANCE")
    @Temporal(TemporalType.DATE)
    private Date dateNaissance;

    @NotNull
    @Column(name="EMAIL")
    private String email;

    public Etudiant() {
        super();
    }

    public Long getIdEtudiant() {
        return idEtudiant;
    }

    public void setIdEtudiant(Long idEtudiant) {
        this.idEtudiant = idEtudiant;
    }

    public Etudiant(String nom, String prenom, Date dateNaissance, String email) {
        super();
        this.nom = nom;
        this.prenom = prenom;
        this.dateNaissance = dateNaissance;
        this.email = email;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public Date getDateNaissance() {
        return dateNaissance;
    }

    public void setDateNaissance(Date dateNaissance) {
        this.dateNaissance = dateNaissance;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

XML文件配置:

@RequestMapping(value="save",method=RequestMethod.POST)
public String  saveetudient(Model md,
        @Valid @ModelAttribute("etudiant") Etudiant et, 
        BindingResult res) {
    if (res.hasErrors()) {
        List <Etudiant> ets=service.listeEtudiants();
        md.addAttribute("etudiants",ets);
        return "etudiant1";
    }
    else {
        service.addEtudiant(et);
        List <Etudiant> ets=service.listeEtudiants();
        md.addAttribute("etudiants",ets);
        return "etudiant1";
    }
}

2 个答案:

答案 0 :(得分:2)

最后我发现了错误, 问题是我没有添加 hibernate-validator jar 我只在dist / lib / required中添加了文件:

  • 同学-1.3.1

  • javax.el-2.2.4 -javax.el-API-2.2.2

  • jboss-logging-3.3.0.Final

  • 验证-API-1.1.0

    所以,如果有人发生这种情况,只需添加:hibernate-validator-5.3.2.Final。

答案 1 :(得分:0)

花了一个小时来研究两个验证教程:

Hibernate Validator Annotations Example

Spring MVC Form Validation Annotation Example

我建议你解决一些问题。

1)验证hibernate验证注释是否有效

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();    
Validator validator = factory.getValidator();   
Set<ConstraintViolation<Etudiant>> constraintViolations = validator.validate(etudiant);

2)您可以创建自定义验证器,例如EtudiantValidator,然后手动验证,如下所示:

@RequestMapping(value="save",method=RequestMethod.POST)
public String  saveetudient(Model md,@ModelAttribute("etudiant") Etudiant etudiant, BindingResult res){
    new EtudiantValidator().validate(etudiant, result);
    ...
}   

但是我深入到你的代码中,我发现你在这个方法上有错误:

@RequestMapping(value="save",method=RequestMethod.POST)
public String  saveetudient(Model md,
        @Valid @ModelAttribute("etudiant") Etudiant et, 
        BindingResult res)

应将其更改为如下所示

@RequestMapping(value="save",method=RequestMethod.POST)
public String  saveetudient(Model md,
        @Valid @ModelAttribute("etudiant") Etudiant etudiant, 
        BindingResult res)

希望这有帮助!