使用: Spring 3.2 portlet MVC与Liferay 5.2.3和Tomcat 6.0.18
我正在尝试创建一个在Set<Type>和String之间来回转换的PropertyEditor。我已经成功地将Set<Type>转到String,使其可以正常工作。但是我不能让属性编辑器被识别为相反的方向。我已经用Type -> String -> Type成功地做到了这一点,但是为Set进行转换却让我摸不着头脑。
我已经确定SetAsText方法永远不会被调用,并且我得到一个运行时错误,表明转换没有完成。关于propertyEditors的信息非常稀少,除了一两个例外,我能找到的唯一模糊相关的问题是4年或更久。
要么我遗漏了一些基本的东西,以至于我看不到它,要么是框架中更深层次的东西,但如果有任何帮助或建议,我会非常感激。
下面是我的控制器中的@InitBinder代码片段:
@InitBinder("formBacking")
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Set.class, "field", new SetEditor(listService, Set.class));
logger.info("FormBacking Binder initialization");
}这是我的PropertyEditor:
public class SetEditor extends PropertyEditorSupport {
protected static Logger logger = Logger.getLogger("PropertyEditor");
private ListService listService;
public SetEditor(ListService listService, Class clazz) {
super(clazz);
this.listService = listService;
}
@Override
public String getAsText() {
Stack<String> returnString = new Stack<String>();
Set<Type> types = new HashSet<Type>();
try {
types = (Set<Type>)this.getValue();
for (Type type:types) {
returnString.push(type.getTypeId().toString());
}
} catch (NullPointerException e) {
logger.info("getAsText is \"\"");
return "";
} catch (Exception e) {
logger.info("getAsText Other Exception: " + e.getMessage());
return "";
}
return "[" + StringUtils.collectionToDelimitedString(returnString,",") + "]"; // a very useful Spring Util
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
Type type = new Type();
Set<Type> result = new HashSet<Type>();
try {
String[] typeArray = text.split("[,]"); //this may not be correct, but I can't get here to debug it!!
for(String type:typeArray) {
if(!type.isEmpty()) {
type = listService.getType(Long.valueOf(text));
result.add(type);
}
}
}catch(NullPointerException e) {
logger.info("SetAsText is \"\" ");
setValue(null);
}
catch(Exception e) {
logger.info("setAsText Other Exception: " + e.getMessage());
}
setValue(result);
}
}Type类的代码片段是:
@Entity(name="Type")
@Table(name="type")
public class Type {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "playListImages",
joinColumns={@JoinColumn(name="superTypeId")},
inverseJoinColumns={@JoinColumn(name="typeId")})
private Set<Type> types = new HashSet<Type>();
getters and setters...发布于 2013-09-11 21:18:44
希望这能帮助任何其他正在努力解决这个问题的人。
经过一些进一步的研究(例如打开跟踪日志记录),Spring注解似乎没有完全注册与Set相关的PropertyEditors (可能是Collections,尽管我还没有验证过)。跟踪显示内置的CustomCollectionEditor被调用(但仅在String -> Type转换时调用),即使我已经注册了自己的编辑器。
imho,这是一个Spring注解错误。解决方法是创建您自己的属性编辑器注册器,并在xxx-portlet.xml配置文件中注册属性编辑器。
例如: project-portlet.xml应该包含以下内容:
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="myPropertyEditorRegistrar" />
</list>
</property>
</bean>
</property>
</bean>
<bean id="myPropertyEditorRegistrar"
class="com.sbeko.slider.util.MyPropertyEditorRegistrar" />注册类:
public class MyPropertyEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry registry)
{registry.registerCustomEditor(Set.class, new TypeEditor(Set.class));
}https://stackoverflow.com/questions/18730007
复制相似问题