【问题标题】:Exception sending id field to @PostMapping将 id 字段发送到@PostMapping 的异常
【发布时间】:2021-11-13 19:09:04
【问题描述】:

我在尝试使用 PostMapping 更新对象时遇到此异常:

引起:org.thymeleaf.exceptions.TemplateProcessingException: 评估 SpringEL 表达式的异常:“libro.id”(模板: “formulario-modificar-libros-p” - 第 18 行,第 15 列)

原因: org.springframework.expression.spel.SpelEvaluationException:EL1007E: 在 null 上找不到属性或字段“id”

表格:

<form th:action="@{/libros/modificar-libro-d/__${libro.id}__}" method="post">
     <input hidden th:value="${libro.id}" name="id">
     <div class="form-group">
         <label>ISBN del libro</label> <input th:value="${libro.isbn}" type="number" class="form-control" name="isbnLibro" required>   
     </div>
     <div class="form-group">
         <label>Título del libro</label> <input th:value="${libro.titulo}" type="text" class="form-control" name="tituloLibro" required>
     </div>
     <div class="form-group">
         <label>Año del libro</label> <input th:value="${libro.anio}" type="number" class="form-control" name="anioLibro">
     </div>
     <div class="form-group">
         <label>Ejemplares del libro</label> <input th:value="${libro.ejemplares}" type="number" class="form-control" name="ejemplaresLibro" required>
     </div>
     <div>
         <label>Autor</label>
         <select class="form-select" aria-label="Default select example" name="nombreAutor">
             <option th:each="autor:${autores}"
                     th:text="${autor.nombre}"
                     th:value="${autor.nombre}"
             >    
             </option>
         </select>
     </div>
     <div>
         <label>Editorial</label>
         <select class="form-select" aria-label="Default select example" name="nombreEditorial">
             <option th:each="editorial:${editoriales}"
                     th:text="${editorial.nombre}"
                     th:value="${editorial.nombre}"
             >    
             </option>
         </select>
     </div>
     <button type="submit" class="btn btn-primary">Aceptar</button>
 </form>

控制器

@PostMapping("/modificar-libro-d/{id}")
public String modificarLibroM(@PathVariable String id, @RequestParam Long isbnLibro, @RequestParam String tituloLibro, @RequestParam Integer anioLibro,
        @RequestParam Integer ejemplaresLibro, @RequestParam String nombreAutor, @RequestParam String nombreEditorial){
    try {
        libroServicio.modificarLibro(id, isbnLibro, tituloLibro, anioLibro, ejemplaresLibro, nombreAutor, nombreEditorial);

        return "redirect:/libros/lista-libros-d";
    } catch (Exception e) {
        return "formulario-modificar-libros-p";
    }
}

@GetMapping("/modificar-libro-d/{id}")
public String formularioModificarLibrosM(@PathVariable String id, ModelMap modelo) {
    modelo.addAttribute("libro", libroServicio.buscarLibroPorId(id));
    modelo.addAttribute("autores", autorServicio.listarAutoresAlta());
    modelo.addAttribute("editoriales", editorialServicio.listarEditorialesAlta());
        
    return "formulario-modificar-libros-p";
}

在实体中,所有字段都有正确的getter和setter。

【问题讨论】:

标签: java spring-boot thymeleaf


【解决方案1】:

看起来您的代码没问题,但是您的图书服务上的 find by id 方法返回给定 id 的 null。在其上放置断点并在调试中运行它。如果必须始终存在 libro 对象,请考虑定义 libroServicio.buscarLibroPorId(id) 方法,以便为缺少 id 引发异常和/或在 thymeleaf 模板中设置错误处理。

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多