Java 将字符串转换为SpringMVC的对象

Java 将字符串转换为SpringMVC的对象,java,jakarta-ee,spring-mvc,Java,Jakarta Ee,Spring Mvc,我在SpringMVC3.0.1框架中将字符串转换为对象时遇到一些问题。当我想使用自定义web编辑器通过表单向PostgreSQL数据库中添加一些数据(添加新产品)时,不会显示任何错误,表单字段会插入到数据库中,并且只会为我添加的产品插入产品类别(在数据库中的category\u types中,我插入了一些类别,它们会显示在视图中)。当我删除控制器中的InitBinder方法时,出现了一个错误,应该是并没有编辑器来转换我想要的内容 以下是我的自定义webeditor的代码: package We

我在SpringMVC3.0.1框架中将字符串转换为对象时遇到一些问题。当我想使用自定义web编辑器通过表单向PostgreSQL数据库中添加一些数据(添加新产品)时,不会显示任何错误,表单字段会插入到数据库中,并且只会为我添加的产品插入产品类别(在数据库中的
category\u types
中,我插入了一些类别,它们会显示在视图中)。当我删除控制器中的InitBinder方法时,出现了一个错误,应该是并没有编辑器来转换我想要的内容

以下是我的自定义webeditor的代码:

package Webeditors;
import java.beans.PropertyEditorSupport;
import Models.CategoryType;
public class CategoryTypeWebEditor extends PropertyEditorSupport {

@Override
public void setAsText(String text) throws IllegalArgumentException { 

    CategoryType a = new CategoryType(); 
    a.setId(Long.parseLong(text));
    setValue(a);  
}      

@Override
public String getAsText() {

    CategoryType type = (CategoryType) getValue();
    return type == null ? null :  Long.toString(type.getId());  
}      
}

我的类别类型是:

package Models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="category_types")
public class CategoryType {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY) 
long id;

String name;

public CategoryType(){}

public CategoryType(long id, String name){

    this.id = id;
    this.name = name;

}

...

@Override
public boolean equals(Object obj) {
    if(obj instanceof CategoryType){
        CategoryType a = (CategoryType)obj;
    return a.getId() == id;
    }
    return false;
}   
}
在我的JavaBeans产品中,我声明:

@ManyToMany(fetch=FetchType.EAGER)
List<CategoryType> type;

public Product(){

    type = new ArrayList<CategoryType>();

}
...

public List<CategoryType> getType() {
    return type;
}

public void setType(List<CategoryType> type) {
    this.type = type;
}
@ManyToMany(fetch=FetchType.EAGER)
列表类型;
公共产品(){
类型=新的ArrayList();
}
...
公共列表getType(){
返回类型;
}
公共void集合类型(列表类型){
this.type=type;
}
在我的控制器中我有如下内容:

@RequestMapping(method = RequestMethod.POST)

public String addProduct(Model model,
        @ModelAttribute("addproduct") Product product, BindingResult result){

....
}
@ModelAttribute("categorytypes")
public List<CategoryType> loadCategoryTypes() {
    List<CategoryType> types = dao.getCategory_Types();
    return types;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(CategoryType.class, new CategoryTypeWebEditor());
}
@RequestMapping(method=RequestMethod.POST)
公共字符串addProduct(模型,
@ModelAttribute(“addproduct”)产品,BindingResult){
....
}
@ModelAttribute(“类别类型”)
公共列表loadCategoryTypes(){
列表类型=dao.getCategory_types();
返回类型;
}
@InitBinder
公共绑定器(WebDataBinder绑定器){
binder.registerCustomEditor(CategoryType.class,新CategoryTypeWebEditor());
}
我是这样展示的:

<form:form cssClass="form-horizontal" method="Post"
        action="addproductform.html" commandName="addproduct">
<form:checkboxes items="${categorytypes}" path="type" itemValue="id" itemLabel="name" delimiter="<br>"/>
<form:errors cssStyle="margin-top: 10px; margin-left: 160px;" cssClass="alert alert-error" element="div" path="type" />
</form:form>

你知道我做错了什么吗?因为我坚持了,所以它没有加上。。
谢谢

好的,你能看到一段可能无法正常运行的代码吗?@Lion它的CategoryTypeWebEditor(我的自定义web编辑器)类导致了一些问题,我假设方法调用
CategoryType type=(CategoryType)getValue()CategoryTypeWebEditor
getAsText()
方法中的code>确实返回类型为
CategoryType
的对象,方法
type.getId()
确实返回正确的id(java.lang.Long
类型的可解析值)以及方法
setAsText的参数(字符串文本)
正在提供?否则没有什么可看的,可能是错误的。请尝试跟踪它。@Lion我试图打印
type.getId()
但方法
公共字符串getAsText()
当我要打印该id时,不会在我的控制台上打印任何内容。然后,您可能需要检查外部依赖项,使其返回正确的id(除了问题中的代码,方法
getValue()
)返回的内容)。跟踪以查看为什么没有返回正确的id值。