shy*_*yam 6 java thymeleaf spring-boot
我有一个简单的模型类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;
}
}
Run Code Online (Sandbox Code Playgroud)
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()
}
Run Code Online (Sandbox Code Playgroud)
我将Spring Boot与Thymeleaf结合使用,以创建通常的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>
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试Product
从控制器中插入结果对象时(我知道我没有在这里显示它,主要是因为我认为这实际上并不是问题的原因),
MySQLIntegrityConstraintViolationException: Column 'category_id' cannot be null
Run Code Online (Sandbox Code Playgroud)
我曾试图改变value
的option
到${category.id}
,但即使这样仍不能解决问题。
我实际上如何使用Thymeleaf将复杂的对象作为POST参数传递给控制器?
与我的最初想法相反,这实际上可能与我有关Controller
,所以这是我的ProductController
:
@RequestMapping(value="/product/save", method=RequestMethod.POST)
public String saveProduct(@Valid @ModelAttribute("newProduct") Product product, ModelMap model) {
productRepo.save(product);
model.addAttribute("productCategories", productCategoryRepo.findAll());
return "admin-home";
}
@RequestMapping(value="/product/save")
public String addProduct(ModelMap model) {
model.addAttribute("newProduct", new Product());
model.addAttribute("productCategories", productCategoryRepo.findAll());
return "add-product";
}
Run Code Online (Sandbox Code Playgroud)
请注意,我已将表单方法更改为 POST。
从 thymeleafs 的角度来看,我可以保证下面的代码应该可以工作。
<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>
Run Code Online (Sandbox Code Playgroud)
前提是你的控制器看起来像这样。
@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);
....
}
}
Run Code Online (Sandbox Code Playgroud)
更新
您的模型应该具有正确的 getter 和 setter 以及正确的名称。例如,对于category
您应该拥有的财产,
public ProductCategory getCategory(){
return category;
}
public void setCategory(productCategory category){
this.category = category;
}
Run Code Online (Sandbox Code Playgroud)
注意-我还没有编译此代码,我从当前的工作项目中提取了它,并将名称替换为您的类名
归档时间: |
|
查看次数: |
5930 次 |
最近记录: |