object ='statutProduits'的验证失败。错误计数:2

时间:2017-12-07 03:25:33

标签: java spring spring-mvc spring-boot thymeleaf

当我尝试提交表单时,我会收到此错误,我需要创建一个对象“status_product”

  

Whitelabel错误页面

     

这个应用程序没有/ error的显式映射,所以你看到了   这是一个后备。

     

Thu Dec 07 04:07:23 GMT + 01:00 2017出现意外错误   (type = Bad Request,status = 400)。验证失败了   对象= 'statutProduits'。错误计数:2

我无法找到错误页面上显示的两个验证错误。我在这一点上很丢失。希望有人可以帮助我,这使我无法在此刻取得进展。

StatutProduits类

package com.example.demo.entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author G557437
 */
@Entity
@Table(name = "STATUT_PRODUITS", catalog = "", schema = "PACKOUT")

public class StatutProduits implements Serializable {
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID_STATUT_PRODUIT")
    private BigDecimal idStatutProduit;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "CODE")
    private String code;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "LIBELLE")
    private String libelle;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "CREE_PAR")
    private String creePar;
    @Column(name = "DATE_CREATION")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateCreation;
    @Size(max = 10)
    @Column(name = "MAJ_PAR")
    private String majPar;
    @Column(name = "DATE_MAJ")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateMaj;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idStatutProduit")
    private List<Produits> produitsList;

    public StatutProduits() {
    }

    public StatutProduits(BigDecimal idStatutProduit) {
        this.idStatutProduit = idStatutProduit;
    }

    public StatutProduits(BigDecimal idStatutProduit, String code, String libelle, String creePar) {
        this.idStatutProduit = idStatutProduit;
        this.code = code;
        this.libelle = libelle;
        this.creePar = creePar;
    }

    public BigDecimal getIdStatutProduit() {
        return idStatutProduit;
    }

    public void setIdStatutProduit(BigDecimal idStatutProduit) {
        this.idStatutProduit = idStatutProduit;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public String getCreePar() {
        return creePar;
    }

    public void setCreePar(String creePar) {
        this.creePar = creePar;
    }

    public Date getDateCreation() {
        return dateCreation;
    }

    public void setDateCreation(Date dateCreation) {
        this.dateCreation = dateCreation;
    }

    public String getMajPar() {
        return majPar;
    }

    public void setMajPar(String majPar) {
        this.majPar = majPar;
    }

    public Date getDateMaj() {
        return dateMaj;
    }

    public void setDateMaj(Date dateMaj) {
        this.dateMaj = dateMaj;
    }

    public List<Produits> getProduitsList() {
        return produitsList;
    }

    public void setProduitsList(List<Produits> produitsList) {
        this.produitsList = produitsList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idStatutProduit != null ? idStatutProduit.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof StatutProduits)) {
            return false;
        }
        StatutProduits other = (StatutProduits) object;
        if ((this.idStatutProduit == null && other.idStatutProduit != null) || (this.idStatutProduit != null && !this.idStatutProduit.equals(other.idStatutProduit))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sagemcom.tn.entities.StatutProduits[ idStatutProduit=" + idStatutProduit + " ]";
    }

}

控制器类

package com.example.demo.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.demo.dao.StatutProduitsRepository;
import com.example.demo.entities.StatutProduits;



@Controller
public class StatutProduitsController {

    @Autowired
    StatutProduitsRepository statutproduitsrepository ;


    @RequestMapping(value="/form", method=RequestMethod.GET)
    public String formStatProduit( Model model) {
        model.addAttribute("statproduit",new StatutProduits());
        return "FormStatutProduit";
    }

    @RequestMapping(value="/save", method=RequestMethod.POST)
    public String save( Model model, StatutProduits statproduit) {
        statutproduitsrepository.save(statproduit);
        return "Confirmation";
    }
}

thymleaf:

<body>
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">Statut d'un produit</div>
            <div class="panel-body">
            <form th:action="@{save}"  method="post">
                <div class="form-group">

                    <label class="control-label">ID</label>
                    <input class="form-control" type="number" name="idStatutProduit"
                            th:value="${statproduit.idStatutProduit}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Code</label>
                    <input class="form-control" type="text" name="code"
                    th:value="${statproduit.code}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Cree par</label>
                    <input class="form-control" type="text" name="creePar"
                          th:value="${statproduit.creePar}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Date creation</label>
                    <input class="form-control" type="date"  name="dateCreation"
                    th:value="${statproduit.dateCreation}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Date maj</label>
                    <input class="form-control" type="date" name="dateMaj"
                    th:value="${statproduit.dateMaj}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Libelle</label>
                    <input class="form-control" type="text" name="libelle"
                    th:value="${statproduit.libelle}"/>
                </div>
                <div class="form-group">

                    <label class="control-label">Maj par</label>
                    <input class="form-control" type="text"  name="majPar"
                     th:value="${statproduit.majPar}"/>
                </div>
                <div>
                <button type="submit" class="btn btn-primary">Save</button>

                </div>
             </form>    
            </div>
     </div>
</div>
</body>

1 个答案:

答案 0 :(得分:1)

在您的情况下,您添加了服务器端验证,但在控制器中,您没有检查这些条件并盲目地将该对象保存到数据库中。而不是像使用BindingResult

那样检查控制器中的条件
 @RequestMapping(value="/save", method=RequestMethod.POST)
 public String save( Model model, @Validated StatutProduits statproduit BindingResult bindingResult) {
     if(bindingResult.hasErrors()){
         return "to same page to shows error fields";
     }
    statutproduitsrepository.save(statproduit);
    return "Confirmation";
 }
相关问题