如何在百里香叶中显示包含另一个对象的对象

时间:2018-02-22 12:39:02

标签: spring thymeleaf

当我想展示与该类别存在多对多关系的产品实体时,我努力构建一个正确的视图。问题始于嵌套类别。我有一个错误:评估SpringEL表达式的异常:" category.name" (productList:31)我将非常感谢你帮助解决这个问题

查看:

<tbody>
            <tr data-th-each="product : ${products}">
                <!--<td><input hidden="hidden" name="id" th:value="${product.id}" /></td>-->
                <td th:text="${product.name}"></td>
                <td th:text="${product.price}"></td>
                <td th:each="category : ${product.categories}"></td>
                <td th:text="${category.name}"></td>
                <td th:text="${product.description}"></td>
                <td th:text="${product.shippingWeight}"></td>
                <td th:text="${product.quantity}"></td>
                <td>delete</td>
            </tr>
            </tbody>

实体:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "products")
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private Long id;
private String name;
private BigDecimal price;
@ManyToMany(cascade = CascadeType.ALL,mappedBy = "products")
private List<Category> categories=new ArrayList<>();
private double shippingWeight;
private boolean isAvailable;
private String description;
private int quantity;
@Transient
private MultipartFile image;

类别:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "category_id")
private Long id;

private String name;

private String description;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "join_category_product", joinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "category_id")},
        inverseJoinColumns = {@JoinColumn(name = "product_id", referencedColumnName = "product_id")})
private List<Product> products=new ArrayList<>();
@Transient
private MultipartFile image;

控制器:

@GetMapping("/productList")
public String productList(Model model) {
    List<Product> product = productService.getProducts();
    model.addAttribute("products",product);
    return "productList";

1 个答案:

答案 0 :(得分:0)

您需要确保Thymeleaf能够理解变量的范围。在您的帖子中,您在for循环之外有category.name。如果categoriesproduct的属性,您可以执行以下操作:

        <tr th:each="product : ${products}">
            <td th:text="${product.name}"></td>
            <td th:text="${product.price}"></td>
            <td>
                <th:block th:each="category : ${product.categories}">
                     <th:block th:text="${category.name}">[category name]</th:block> 
                     <br>
                </th:block>
            </td>
            <td th:text="${product.description}"></td>
            <td th:text="${product.shippingWeight}"></td>
            <td th:text="${product.quantity}"></td>
            <td>delete</td>
        </tr>

如果某个产品没有类别,请注意上述内容仍会创建<td>。这很可能是你想要的。

除此之外:在您的代码之间提供默认值也是有意义的。这样,您可以直接在浏览器中打开文件(没有容器),仍然可以看到它的布局。这是使用Thymeleaf的一个重要原因。我在上面的[category name]示例中添加了此内容。

否则,你可以做更短的版本:

<td>[[${product.name}]]</td>