PropertyEditor与Set类型有关

时间:2013-09-10 22:51:32

标签: propertyeditor spring-mvc-initbinders

使用:带有Liferay 5.2.3和Tomcat 6.0.18的Spring 3.2 portlet MVC

我正在尝试创建一个PropertyEditor,以便在Set<Type>String之间进行转换,然后再返回。我已成功Set<Type>String无问题地工作。但我无法让属性编辑器反向识别。我已成功完成Type - &gt; String - &gt; 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...

1 个答案:

答案 0 :(得分:2)

希望这可以帮助其他任何一个努力解决这个问题的人。

经过一些进一步调查(即启用跟踪记录)后,Spring Annotations似乎没有完全注册与PropertyEditors关联的Set(通常可能Collections ,虽然我还没有证实)。 Trace显示内置的CustomCollectionEditor被调用(但仅限于String - &gt; Type次转化),即使我已经注册了自己的编辑器。

这个,imho,是一个Spring Annotations bug。这个完全按照我的预期工作的工作是创建自己的属性编辑器注册器并在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));
}