java - 对象 ='statutProduits' 验证失败。错误计数 : 2

标签 java spring spring-mvc spring-boot thymeleaf

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

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Dec 07 04:07:23 GMT+01:00 2017 There was an unexpected error (type=Bad Request, status=400). Validation failed for object='statutProduits'. Error count: 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 + " ]";
    }

}

Controller 类:

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";
    }
}

thymeleaf :

<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>

最佳答案

就您而言,您已经添加了服务器端验证,但在 Controller 中,您没有检查该条件并盲目将该对象保存到数据库中。而不是使用 BindingResult

检查 Controller 中的条件
 @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";
 }

关于java - 对象 ='statutProduits' 验证失败。错误计数 : 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47687094/

相关文章:

java - 具有默认用户的 Spring MVC Web 应用程序

java - Spring/Hibernate 获取包含列表的对象列表

java - 我的类中变量和函数的可访问性

java - 在 Java 8 中使用三元运算符的泛型编译错误,但在 Java 7 中没有

java - 使用 Spring Boot 在多模块项目中加载静态内容时出错

java - 在 spring 中动态注入(inject)列表属性中的值

java - 当 @Autowiring 使用不同的泛型类型时,Spring 不会创建不同的 bean

java - 如何使用 ModelAndview 将模型(bean)值从 Controller 传递到 jsp 页面?

java - Thymeleaf:在 thymeleaf 中创建动态 URL 查询字符串

java - 与 Java 一起使用时,Python 提供了哪些额外功能?