尝试更新我的数据库中的原始文件时,BeanResult'fileName'的BindingResult或普通目标对象都可以作为请求属性“

问题描述

在尝试更新数据库中的一行时遇到此异常。我在Google进行了很多研究,发现我应该添加已经完成的@modelattribute

我还发现我需要在@modelattribute之后添加bindind结果,但这也不起作用,因此我将其删除。我使用JPA进行持久性处理数据,使用spring boot和thymeleaf进行视图处理。

  1. 这些是我的控制器之一,用于更新和渲染视图

        @GetMapping("/edit/{id}")
       public ModelAndView UpdateList(@PathVariable(name="id") String id) {
     ModelAndView mav = new ModelAndView("updateList");
     com.pfe.ClientRest.model.Files files = fileServ.get(id);
     mav.addobject("Files",files);
     return mav ;
     }    
     @PostMapping("/Save")
     public String saveRepport(@modelattribute("Files") com.pfe.ClientRest.model.Files dbfile) {
    
     fileServ.save(dbfile);
    
    
    
     return "/redirect:/ListFile";
     }
    
  2. 这是我的实体类我有getter的设置方法和构造方法

    @Table( name="Files")
    @Entity
    public class Files {
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid",strategy = "uuid2")
    @Id
    private String id;
    private String FileName;
    private String Verif;
    public String getId() {
    return id;
    }
    

这是我的模板。

     <div class="container">
     <h1> Modifier les @R_848_4045@ions du Rapports</h1>
     <form action="#" th:action="@{/Save}" th:objects="${Files}"  
     method="post" >
     <input type="text" th:field=*{id} readonly="readonly"/>
     <input type="text" th:field="*{fileName}" placeholder="Nom du Fichier" 
     class="form-control mb-4 
     col-4">
     <input type="text" th:field="*{verif}" placeholder="Accepted/Rejected" 
     class="form-control mb-4 
     col-4">
    <button type="submit" class="btn btn-info col-2"> Mettre à jour</button>
    </form>
    </div>

解决方法

html页面和实体类中的字段名称不匹配。这些区分大小写。所以正确的应该是

private String fileName;
private String verif;