2016-08-19 96 views
1

我有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 
+0

我不關於這樣的框架;但如果你期望將一個字符串轉換爲一個類別...也許這個類別類至少需要一個帶字符串的構造函數? – GhostCat

回答

0

toString()不會幫助你在這種情況下,你會需要一個屬性編輯或格式化或便利着想。 rter註冊將字符串數據轉換爲您要發送到服務器的類別。在給你任何代碼示例之前,我需要看看你的JSP文件是怎麼樣的,但this應該是一個很好的開始。

+0

0

這裏ITEM表是多餘的。因爲Item類只包含一個屬性,並且它是一對一的關係。所以你應該避免這個實體類。還有一件事,每個實體類都應該有一個帶有@Id註釋的主鍵。

清楚地注意到Item類帶有Category類型屬性,並且從jsp表單中沒有Category類型屬性。舉一個例子:

<form action="action_url" method="POST"> 
    <input type="text" name="a"/> 
    <input type="text" name="b"/> 
    <input type="text" name="c"/> 
    <input type="submit" value="Submit"/> 
</form> 

這個JSP生成你的實體/模型類謹

@Entity 
@Table(name = "CATEGORY") 
public class Category implements Serializable { 

    @Id 
    @GeneratedValue 
    @Column(name = "ID") 
    private Long id; 

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

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

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

    ...Getter and Setter here.... 
} 

這是必須確保表單元素的名稱和型號屬性名稱是相同的,如果你想自動綁定他們。但是在你的jsp頁面中沒有名爲「category」的元素,並且你不能創建這種類型的表單元素。原因Category是用戶定義的類,而html不知道這種類型的元素。我希望你明白這一點。

您應該使用Category對象來綁定元素。 這樣的:

@RequestMapping(value = "/add", method = RequestMethod.POST) 
public String savePorduct(Model model, @ModelAttribute("category") Category category, BindingResult result) { 
--other logic here-- 
} 

我希望這可以解決您的問題。

0

讓我們清楚一件事。如果你想綁定嵌套的對象字段,你必須設置你的html元素的'name'屬性,如name =「object.field」 這裏你試圖綁定你的select元素(它是String)的值Item類中的類別字段(即類型Category)。這不起作用。

嘗試

path = "category.categoryName" 

,看看它是否工作。

總之,你應該有一個項目實體的主鍵。

相關問題