我有Item類和Category類。 Item類具有對Category類的引用。無法將字符串轉換爲我的自定義數據類型類別
Item Class如下。
@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
文件我已經使用以下代碼來顯示在一個選擇框的類別。
有錯誤「> 分類
<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
我不關於這樣的框架;但如果你期望將一個字符串轉換爲一個類別...也許這個類別類至少需要一個帶字符串的構造函數? – GhostCat