首页 > 解决方案 > 如何从 thymeleaf 中的数据库中检索表值,使用嵌套属性休眠?

问题描述

我正在使用 Thymeleaf 和休眠创建一个简单的 Spring Boot Web 应用程序。因此计划从产品实体中展示产品类别,其中每个类别属于多个产品,一个产品只有一个类别。

希望显示所有产品,但有些我无法使用嵌套属性显示产品类别。

[[${product.category.name}]]

它给出了以下错误

此应用程序没有显式映射 /error,因此您将其视为后备。

Sun Jan 31 03:46:01 CET 2021 出现意外错误(类型=内部服务器错误,状态=500)。模板解析时出错(模板:“类路径资源[templates/products.html]”)org.thymeleaf.exceptions.TemplateInputException:模板解析时出错(模板:“类路径资源[templates/products.html] ") 在 org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) 在 org.thymeleaf.engine.TemplateManager.parseAndProcess 的 org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) (TemplateManager.java:666) 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) 在 org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) 在 org.thymeleaf.spring5.view。

@Entity
public class Product {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    
    private Integer id;
    @Column(length=128, nullable=false,unique=true)
    private String name;
    private float price;
    
    
    
    @ManyToOne
    @JoinColumn(name = "category_id")
    public Category category ;


    
    public Product() {}

    public Product(Integer id) {
        super();
        this.id = id;
    }

    public Integer getId() {
        return id;
    }



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



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public float getPrice() {
        return price;
    }



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



    public Category getCategory() {
        return category;
    }



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

}

实体关系图

@Controller
public class ProductController {
    
    @Autowired
    private ProductRepository ProductRepo;
    @Autowired
    private CategoryRepository categoryRepo;
    
    @GetMapping("products/new")

    public String showProductForm(Model model) {
        
    List<Category> listCategories=categoryRepo.findAll();   
    model.addAttribute("product", new Product());   
    model.addAttribute("listCategories", listCategories);   

    return "product_form";      
        
    }

    
    @PostMapping("/products/save")
    public String saveProduct(Product product) {
        
        ProductRepo.save(product);
        return "redirect:/";
        
    }
    
    
    @GetMapping("/products")
    public String listProducts(Model model) {
        
        List <Product> listProducts=ProductRepo.findAll();
        model.addAttribute("listProducts",listProducts);
        
        return "products";
    }
    
    
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>List Categories </title>
<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" />
</head>
<body>
<div class="container text-center">
<div><h1> Products List</h1></div>

<div>

    <table class="table table-bordered">
        <thead class="thead-dark">
        <th>ID</th>
        <th>Name</th>
        <th>Price</th>
        <th>Category</th>
        
        </thead>
    
        <tbody>
          <th:block th:each="product : ${listProducts}">
                        <tr>
                            <td>[[${product.id}]]</td>
                            <td>[[${product.name}]]</td>
                            <td>[[${product.price}]]</td>
                        <td>[[${product.category.name}]]</td>
                        //error in displaying product category name 
                
              
            </tr>
        </tbody>
    </table>


</div>



</div>
</body>
</html>

标签: javaspring-boothibernatethymeleaf

解决方案


错误在这里:

@ManyToOne
@JoinColumn(name = "category_id")
public Category category ;

你可以在产品实体上试试这个:

@ManyToOne(optional=true)
private Category category;

在类别实体上:

@OneToMany(mappedBy="category")
private List<Product> product;

推荐阅读