使用Spring PropertyEditors将逗号分隔的字符串绑定到域对象

时间:2013-05-28 02:48:27

标签: spring-mvc

我正在尝试将另一个名为Expense的域对象中的文本字段(以逗号分隔的标记名称列表,Tag为我的域对象)绑定到变量,该变量是Tag对象的List。因此,用户在表单中输入费用项目的标签列表,并将其绑定到费用域对象中的标签项目集合。到目前为止,我在代码中所做的是:

我的jsp文件有:

<tr>
  <td>Tags</td>
  <td><form:input path="tags" type="text" name="tags" id="tags_formfield" /></td>
</tr>

我的域名对象有:

    //bi-directional many-to-many association to Tag
@ManyToMany()
@JoinTable(name="EXPENSES_X_TAGS",
joinColumns= @JoinColumn(name="EXPENSE_ID", referencedColumnName="ID"), 
inverseJoinColumns= @JoinColumn(name="TAG_ID", referencedColumnName="ID"))

private List<Tag> tags;

我的控制器有:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(List.class, new TagsEditor());
}

我也尝试过:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Tag.class, new TagsEditor());
}

和     我的房产编辑是:
    package com.transience.sandbox.converters;         import java.beans.PropertyEditorSupport;         import java.util.ArrayList;         import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.transience.sandbox.domain.Tag;
import com.transience.sandbox.services.ITagService;

public class TagsEditor extends PropertyEditorSupport {

    @Autowired
    ITagService tagService; 
    protected final Log logger = LogFactory.getLog(getClass());

    @Override
    // Converts a comma separated String of tagNames to a List<Tag> (when submitting form)
    public void setAsText(String stringOfTagNames) {
        List<Tag> tags = null;

        String[] tagNames = stringOfTagNames.split(",");
        logger.info("Looping through tagNames now...");
        for(String tagName : tagNames) {
            logger.info("tag name: " + tagName);
            Tag tag = tagService.findByTagName(tagName);
            tags.add(tag);
        }       
        logger.info("Trying to setValue now...");
        this.setValue(tags);
        logger.info("Value of List of Tags set successfully...");
    }




}

但是我在表单提交中遇到以下异常:

Error 500--Internal Server Error

    org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'expense' on field 'tags': rejected value [booze,lunch]; codes [methodInvocation.expense.tags,methodInvocation.tags,methodInvocation.java.util.List,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [expense.tags,tags]; arguments []; default message [tags]]; default message [Property 'tags' threw exception; nested exception is java.lang.NullPointerException]
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:110)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:74)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:155)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)

我很确定我做的事情根本不正确,但我做错了什么?我知道转换器方法,但我很想知道如何使用PropertyEditors来完成这项工作

谢谢, 桑杰

1 个答案:

答案 0 :(得分:2)

我通过将valueOf(String)添加到域对象来解决它(对于向后转换,toString也很重要)。 不知何故,Spring已经能够将csv转换为List。 在这里找到答案:How to bind a comma separated string to a Java collection (List) in Spring form?