我正在尝试将一个文本字段(一个以逗号分隔的标签名称列表,标签是我的域对象)绑定到一个变量,该变量是一个标签对象列表,位于另一个名为 Expense 的域对象中。因此,用户在表单中输入费用项目的标签列表,这将绑定到费用域对象中的标签项目集合。到目前为止,我在代码中所做的是:
我的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;导入 java.beans.PropertyEditorSupport;导入 java.util.ArrayList;导入 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 来完成
谢谢,桑杰