当前位置: 首页 > 知识库问答 >
问题:

IllegalStateException:bean名称'ecole'的BindingResult和普通目标对象都不能作为请求属性使用

罗金林
2023-03-14
@Entity

公共类EcoleProvenance实现Serializable{

@Id
@Column(length = 8)
@NotEmpty
@Size(min=4,max=25)
private String codEcole;
private String Designation;
private Date created;


@OneToMany(mappedBy = "ecole", fetch = FetchType.LAZY,
        cascade = CascadeType.ALL)
private List<Candidat>candidat;


public EcoleProvenance() {
    super();
}


public EcoleProvenance(String codEcole, String designation, Date created,
        List<Candidat> candidat) {
    super();
    this.codEcole = codEcole;
    Designation = designation;
    this.created = created;
    this.candidat = candidat;
}


public String getCodEcole() {
    return codEcole;
}


public void setCodEcole(String codEcole) {
    this.codEcole = codEcole;
}


public String getDesignation() {
    return Designation;
}


public void setDesignation(String designation) {
    Designation = designation;
}


public Date getCreated() {
    return created;
}


public void setCreated(Date created) {
    this.created = created;
}

这是我的控制器

// recuperation d'objet ecole
    @GetMapping("/ecolRegister")
    public String ecolRegister(Model model) {
        model.addAttribute("ecole", new EcoleProvenance());
        return "Inscription/detailsInfoCand";
    }



    //register un candidat
        @PostMapping("/saveEcole")
        public String saveEcole(@Valid EcoleProvenance e, BindingResult bindignResult, Model model) throws Exception {

            if(bindignResult.hasErrors()) {
                return "Inscription/detailsInfoCand";
            }


             if(isPresent(e.getCodEcole())) {
                    model.addAttribute("existe", true);
                    return "Inscription/detailsInfoCand";
             }

             ecoleRepository.save(e);
            return "redirect:detailsInfoCand";
        }





        //tester s'il le recordexiste deja
    private boolean isPresent(String codEcole) {
        // TODO Auto-generated method stub
        Optional<EcoleProvenance> eco = ecoleRepository.findById(codEcole);
            if(eco!=null) {
                return  true;
            }
        return false;
    }

我的html

<div class="modal-body">
                            <form th:action="@{/saveEcole}"  th:object="${ecole}" method="post">
                                <div class="field item form-group">
                                  <label class="col-form-label col-md-3 col-sm-3  label-align">Code Ecole <span
                                      class="required">*</span></label>
                                  <div class="col-md-6 col-sm-6">
                                    <input type="text"  th:field="*{codEcole}" required="required" class="form-control"/>
                                     <span class="text-danger"  th:if="${#fields.hasErrors('codEcole')}" th:errors=*{codEcole}></span>
                                  </div>
                                </div>

                                <div class="field item form-group">
                                  <label class="col-form-label col-md-3 col-sm-3  label-align">Description<span
                                      class="required">*</span></label>
                                  <div class="col-md-6 col-sm-6">
                                    <textarea class="form-control"  th:field="*{Designation}" rows="5" cols="40"></textarea>                                                                    
                                    </div>
                                </div>
                                <div class="ln_solid">
                                  <div class="form-group">
                                    <div class="col-md-6 offset-md-3">
                                <button type='submit' class="btn btn-primary">Submit</button>
                                <button type='reset' class="btn btn-success">Reset</button>
                                    </div>
                                  </div>
                                </div>
                              </form>
                          </div>

任何时候,如果我逃跑,我会得到这个答案

2020-06-08 04:48:28.465错误11644--[nio-8080-exec-9]O.A.c.c.c.[.[/].[dispatcherServlet]:路径为[]的上下文中servlet[dispatcherServlet]的servlet.service()引发异常[请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateProcessingException:执行处理器“org.thymeleaf.Spring4.processor.SpringInputGeneralFieldTagProcessor”(模板:“Enterprisation/DetailsInFocancor”-第96行,第

2020-06-08 04:48:28.493错误11644--[nio-8080-exec-9]S.ErrorMVCautoConfiguration$StaticView:无法呈现请求[/SaveEcole]和异常的错误页[执行处理器'org.thymeleaf.spring4.processor.springInputGeneralFieldTagProcessor'(模板:“Enterprisation/DetailsInFocand'-第96行,第49栏)期间出错],因为响应已提交。因此,响应可能具有错误的状态代码。

共有1个答案

邢法
2023-03-14

您需要指定在get处理程序中将命令对象放入模型时使用的模型属性名。

更改为

@PostMapping("/saveEcole")
public String saveEcole(@Valid @ModelAttribute("ecole") EcoleProvenance e, 
                        BindingResult bindignResult, 
                        Model model) throws Exception {

    // your code here   
}

@PostMapping("/saveEcole")
public String saveEcole(@Valid @ModelAttribute EcoleProvenance ecole, 
                        BindingResult bindignResult, 
                        Model model) throws Exception {

    // your code here   
}
 类似资料: