首页 > 解决方案 > 将继承的类放入对象标签thymeleaf

问题描述

我的百里香代码有问题。我有三个类,抽象产品、子车辆和子结构。在我看来,thymeleaf 只支持在控制器方法中传递的类。在这种情况下,它是 Vehicle 的实例。问题是如果我想使用类结构的实例,那么我必须写 th:object=${structure} 而不是 th:object=${product}

问题通过以下方式暂时解决:

<form action="#" th:action="@{/save}" th:object="${product}" method="post">

<form action="#" th:action="@{/save}" th:object="${vehicle}" method="post">

控制器中的方法

    @RequestMapping(value="/edit/{id}")
    public ModelAndView editProduct(@PathVariable(name = "id") Long id) {
        ModelAndView mav = new ModelAndView("editProduct");
        
        Product product = new Vehicle(); //Product is abstract class, Vehicle is child class of Product
        product.setName("Name");
        product.setId(1L);
        product.setPrice(200);

        mav.addObject(product);
        return mav;
    }

百里香代码

    <form action="#" th:action="@{/save}" th:object="${product}" method="post">
    <table>
        <tr>
            <label for="id">Id:</label>
            <input type="text" th:field="*{id}" id="id" name="id" readonly="readonly">
        </tr>
        <!-- I would use this for vehicle
        <tr>
            <label for="brand">Brand:</label>
            <input type="text" th:field="*{brand}"  id="brand" name="brand">
        </tr>
        -->
        <!-- I would use this for structure
        <tr>
            <label for="material">Material:</label>
            <input type="text" th:field="*{material}"  id="material" name="material">
        </tr>
        -->
        <tr>
            <label for="name">Name:</label>
            <input type="text" th:field="*{name}"  id="name" name="name">
        </tr>
        <tr>
            <label for="price">Price:</label>
            <input type="number" th:field="*{price}"  id="price" name=""price"">
        </tr>
        <button type="submit">Save</button>
    </table>
    </form>

产品类别

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Product {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column
    private String name;

    @Column
    private float price;
    
    public Product() {
        
    }
    
    public Long getId() {
        return id;
    }
    public void setId(Long 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;
    }
}

车辆类

@Entity
public class Vehicle extends Product {

    @Column
    private String brand; 
    
    public Vehicle() {
        super();
    }
    
    public String getBrand() {
        return this.brand;
    }
    
    public void setBrand(String brand) {
        this.brand = brand;
    }
}

@Entity 公共类结构扩展产品 {

@Column
private String material; 

public Structure() {
    super();
}

public String getMaterial() {
    return this.material;
}

public void setMaterial(String material) {
    this.material= material;
}

}

标签: javaspringthymeleaf

解决方案


关键是这一行:

mav.addObject(product);

这会将“product”添加到名为“product”的模型中(因为 Product 是类的名称)。

如果您想为模型中的对象指定您选择的特定名称,请改用此方法:

mav.addObject("product", product);

现在,“产品”对象是什么类型已经无关紧要了。它在模型中总是被称为“产品”。

见这里:https ://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-modelattrib-methods

当未明确指定名称时,将根据 Object 类型选择默认名称,如约定的 javadoc 中所述。您始终可以通过使用重载的 addAttribute 方法或通过 @ModelAttribute 上的 name 属性(用于返回值)来分配显式名称。


推荐阅读