使用Thymeleaf发布多对一关系的数据

时间:2015-04-15 09:10:11

标签: java spring-boot thymeleaf

我有一个简单的模型类Product,它与ProductCategory表现出多对一的关系:

产品类别:

@Entity
@Table(name="product")
public class Product {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;

@Column(name="name")
private String name;

@Column(name="description")
private String description;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="category_id")
private ProductCategory category;

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 String getPdfUrl() {
    return pdfUrl;
}

public void setPdfUrl(String pdfUrl) {
    this.pdfUrl = pdfUrl;
}

public ProductCategory getCategory() {
    return category;
}

public void setCategoryId(ProductCategory category) {
    this.category = category;
}

}

ProductCategory类

@Entity
@Table(name="product_category",uniqueConstraints={@UniqueConstraint(columnNames="name")})
public class ProductCategory {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;

@Column(name="name")
private String name;

@OneToMany(fetch=FetchType.LAZY, mappedBy="category")
private Set<Product> products = new HashSet<Product>(0);

// getters() & setters()

}

我正在使用Thymeleaf的Spring启动来为通常的CRUD操作创建必要的表单。

这是我的html页面的基本部分,我用它将新的Product对象添加到数据库中。

<form action="#" th:action="/product/save" th:object="${newProduct}" method="POST">
  <input type="text" th:field="*{name}" />
  <input type="text" th:field="*{description}" />
  <select th:field="*{category}">
    <option th:each="category: ${productCategories}" th:value="${category}" th:text="${category.name}" />
  </select>
  <button type="submit">Submit</button>
</form>

问题是,当我尝试从控制器插入生成的Product对象时(我知道我还没有在这里显示它,主要是因为我不认为这实际上是原因)问题),有一个

MySQLIntegrityConstraintViolationException: Column 'category_id' cannot be null

我尝试将value的{​​{1}}更改为option,但即使这样也无法修复。

简而言之

如何使用Thymeleaf将复杂对象作为POST参数传递到控制器?

更新

与我的初步想法相反,这实际上可能与我的${category.id}有关,所以这是我的Controller

ProductController

1 个答案:

答案 0 :(得分:2)

请注意,我已将表单方法更改为POST。

从百里香的角度来看,我可以保证下面的代码应该有效。

<form method="POST" th:action="@{/product/save}" th:object="${newProduct}">
    ....
    <select th:field="*{category}" class="form-control">
       <option th:each="category: ${productCategories}" th:value="${category.id}" th:text="${category.name}"></option>
    </select>

如果您的控制器看起来像这样。

@RequestMapping(value = "/product/save")
public String create(Model model) {
    model.addAttribute("productCategories", productCategoryService.findAll());
    model.addAttribute("newproduct", new Product()); //or try to fetch an existing object
    return '<your view path>';
}

@RequestMapping(value = "/product/save", method = RequestMethod.POST)
public String create(Model model, @Valid @ModelAttribute("newProduct") Product newProduct, BindingResult result) {
    if(result.hasErrors()){
        //error handling  
        ....
    }else {
        //or calling the repository to save the newProduct
        productService.save(newProduct);
        ....
    }
}

<强>更新

您的模型应该具有正确名称的正确的getter和setter。例如,对于您应该拥有的属性category

public ProductCategory getCategory(){
    return category;
}

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

注意 - 我还没有编译这段代码,我从当前的工作项目中提取了它并用你的类名替换名称