【问题标题】:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on nullorg.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“id”
【发布时间】:2021-08-22 03:02:20
【问题描述】:

我正在使用带有 Thymeleaf 模板的 Spring,当我尝试使用 deleteById() 方法删除数据库中的对象时遇到错误:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null。我不明白为什么它告诉“id”字段为空,如果在数据库中我可以看到“id”字段不为空。我不知道是什么问题...请帮助!

解决方案: 在文件 index.html 中的 <td th:text="${product.quantity}">Quantity</td></tr> 行中,我有多余的,所以我只需要删除它。

模型类:


import javax.persistence.*;

@Entity
@Table (name = "product")
public class Product {
    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private int id;
    private String category;
    private String brand;
    private String model;
    private Double price;
    private Integer quantity;

    public Product(){

    }

    public Product(String category, String brand, String model, Double price, Integer quantity) {
        this.category = category;
        this.brand = brand;
        this.model = model;
        this.price = price;
        this.quantity = quantity;
    }

    public Product(int id, String category, String brand, String model, Double price, Integer quantity) {
        this.id = id;
        this.category = category;
        this.brand = brand;
        this.model = model;
        this.price = price;
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", category='" + category + '\'' +
                ", brand='" + brand + '\'' +
                ", model='" + model + '\'' +
                ", price='" + price + '\'' +
                ", quantity='" + quantity + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
}

控制器:

package com.example.CRUDApp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@org.springframework.stereotype.Controller
public class Controller {
    private final ProductRepository productRepository;

    @Autowired
    public Controller(ProductRepository productRepository){
        this.productRepository = productRepository;
    }

    @RequestMapping("/")
    public String showAllProducts(Model model){
        model.addAttribute("products", productRepository.findAll());
        return "index";
    }

    @RequestMapping("/addProduct")
    public String add(){
        return "add-product";
    }

    @RequestMapping("/add")
    public String addProduct(@RequestParam ("category") String category,
                             @RequestParam ("brand") String brand,
                             @RequestParam ("model") String model,
                             @RequestParam ("price") Double price,
                             @RequestParam ("quantity") Integer quantity,
                             Model mvcModel
    ) throws Exception {
        Product product = new Product(category, brand, model, price, quantity);
        productRepository.save(product); // save to database!!!
        mvcModel.addAttribute("products", product);
        return "index";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("id") Integer id, Model model) {
        productRepository.deleteById(id);
        model.addAttribute("products", productRepository.findAll());
        return "index";
    }
}

使用 Thymeleaf 的 html 文件:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Products</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
              integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>
<body>
<p align="center"><h2>Products</h2></p> <br>
<table class="table table-striped">
    <tr><th>ID</th><th>Category</th><th>Brand</th><th>Model</th><th>Price, $</th><th>Quantity</th></tr>
    <tr th:each="product: ${products}">
        <td th:text="${product.id}">ID</td>
        <td th:text="${product.category}">Category</td>
        <td th:text="${product.brand}">Brand</td>
        <td th:text="${product.model}">Model</td>
        <td th:text="${product.price}">Price, $</td>
        <td th:text="${product.quantity}">Quantity</td></tr>
        <td><a href="#" th:href="@{/delete(id=${product.id})}" class="btn btn-danger">
            <i class="fa fa-trash-o fa-lg" ></i>Delete</a></td>
    </tr>
</table>
<center>
        <a href="http://localhost:8080/addProduct">Add product</a>
        <br>
        <a href="http://localhost:8080/">Refresh</a>
</center>
</body>
</html>

【问题讨论】:

  • 您可以编辑您的问题并添加异常的完整堆栈跟踪吗?
  • 是每次删除产品时还是添加新产品后才会出现这种情况?因为添加代码看起来很奇怪:mvcModel.addAttribute("products", product); 您正在将单个产品添加到属性“产品”中,该属性应该是产品列表。因此,您正在用无法在 th:each 中迭代的单个产品替换产品列表
  • @SergeyVasnev 我更改了代码:code List ProductList = productRepository.findAll(); model.addAttribute("产品", ProductList);这里 mvcModel.addAttribute("product", product);现在当我添加产品时它很好,但是当我试图到达 index.html 页面时,突然我得到了同样的错误。我删除了codetd> 删除这一行,我对code行中的“id”字段没有问题@ ID

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


【解决方案1】:

在文件 index.html 的 &lt;td th:text="${product.quantity}"&gt;Quantity&lt;/td&gt;&lt;/tr&gt; 行中,我有多余的 &lt;/tr&gt; 结束标记,所以我只需要将它删除。

【讨论】:

    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2018-03-31
    • 2018-08-07
    • 2016-11-16
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多