2011-07-06 87 views
1

我可以使用SpringMVC中的属性编辑器来自定义转换请求参数。例如,为了请求参数转换成的Foo实例下面SpringMVC请求参数转换

public class Foo { 
    private val; 

    public Foo(String val) { 
     this.val = val; 
    }   
    public getVal() { 
     return val; 
    }  
} 

我可以定义一个属性编辑器

public class FooPropertyEditor extends PropertyEditorSupport { 

    void setAsText(String paramValue) { 
     value = new Foo(paramValue); 
    } 

    public String getAsText() { 
     return ((Foo) value).getVal(); 
    } 
} 

和注册这个从字符串执行转换到一个Foo

public class CustomEditorRegistrar implements PropertyEditorRegistrar { 

    public void registerCustomEditors(PropertyEditorRegistry reg) { 
     reg.registerCustomEditor(Foo.class, new FooPropertyEditor()); 
    } 
} 

是否可以使用属性编辑器来转换多值参数,如

foo=foo1&foo=foo2&foo=foo3 

List<Foo>。假设我已经写了一个合适的属性编辑器FooListPropertyEditor,我不认为我可以用它注册:

public void registerCustomEditors(PropertyEditorRegistry reg) { 
    reg.registerCustomEditor(List<Foo>.class, new FooListPropertyEditor()); 
} 

因为据我所知List<Foo>.class无效语法

回答

0

看看org.springframework。 beans.propertyeditors.CustomCollectionEditor