无法将字符串转换为我的自定义数据类型类别

问题描述:

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

toString()不会帮助你在这种情况下,你会需要一个属性编辑或格式化或便利着想。 rter注册将字符串数据转换为您要发送到服务器的类别。在给你任何代码示例之前,我需要看看你的JSP文件是怎么样的,但this应该是一个很好的开始。

+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-- 
} 

我希望这可以解决您的问题。

让我们清楚一件事。如果你想绑定嵌套的对象字段,你必须设置你的html元素的'name'属性,如name =“object.field” 这里你试图绑定你的select元素(它是String)的值Item类中的类别字段(即类型Category)。这不起作用。

尝试

path = "category.categoryName" 

,看看它是否工作。

总之,你应该有一个项目实体的主键。