我有 Item 类和 Category 类。Item 类具有对 Category 类的引用。
项目类别如下。
@Entity
@Table(name = "ITEMS")
public class Item {
@OneToOne
private Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
类别类别如下。
package com.easypos.models;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
@Entity
@Table(name="category")
public class Category implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
@Size(min = 2, max = 30)
@Column(name = "CATEGORY_NAME", nullable = false)
private String categoryName;
@Column(name = "CATEGORY_REMARK", nullable = true)
private String categoryDescription;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName.trim();
}
public String getCategoryDescription() {
return categoryDescription;
}
public void setCategoryDescription(String categoryDescription) {
this.categoryDescription = categoryDescription;
}
@Override
public String toString() {
return this.categoryName;
}
}
我的控制器方法如下
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String create(Model model) {
item = new Item();
model.addAttribute("title", "Add Item");
model.addAttribute("categories", categoryService.findAll());
model.addAttribute("suppliers", supplierService.findAll());
model.addAttribute(item);
return "item/create";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String savePorduct(Model model, @ModelAttribute("item") @Valid Item item, BindingResult result,
RedirectAttributes redirectAttributes) {
itemValidator.validate(item, result);
if (result.hasErrors()) {
model.addAttribute("title", "Add Item");
model.addAttribute("categories", categoryService.findAll());
model.addAttribute("suppliers", supplierService.findAll());
return "item/create";
}
redirectAttributes.addFlashAttribute("message", "Item successfully saved.");
itemService.saveitem(item);
return "redirect:/item/";
}
在我的Jsp
文件中,我使用以下代码在选择框中显示类别。
has-error "> 类别
<div class="col-sm-9">
<sf:select path="category" cssClass="form-control">
<sf:options items='${categories}' itemValue='id'/>
</sf:select>
<p><sf:errors path="category" /></p>
</div>
</div>
在我的Jsp
文件中,当我提交表单时,我收到“找不到匹配的编辑器或转换策略”的错误 - 完整的错误日志如下:
Failed to convert property value of type [java.lang.String] to required type [com.easypos.models.Category] for property category; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.easypos.models.Category] for property category: no matching editors or conversion strategy found