我只是想知道,我可以将URI字符串转换为另一种对象类型吗?

    @RequestMapping(value="/key/{keyDomain}", method=RequestMethod.GET)
    public String propertyEditor(@PathVariable(value="keyDomain") KeyDomain key, Model model){
        model.addAttribute("key", key);
        return "propertyEditor";
    }

在这里我的配置

<beans:bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <beans:property name="customEditors">
            <beans:map>
                <!-- <beans:entry key="com.template.baseline.domain.KeyDomain" value="com.template.baseline.propertyEditor.KeyPropertyEditor"/>  -->
                <beans:entry key="com.template.baseline.domain.KeyDomain">
                    <beans:ref bean="keyDomainPropertyEditor"  />
                </beans:entry>
            </beans:map>
        </beans:property>
    </beans:bean>

<!-- key domain property editor bean -->
<beans:bean id="keyDomainPropertyEditor"  class="com.template.baseline.propertyEditor.KeyPropertyEditor">
    <beans:property name="keyDomain">
        <beans:bean class="com.template.baseline.domain.KeyDomain" />
    </beans:property>   
</beans:bean>

和ProperityEditor类:

public class KeyPropertyEditor extends PropertyEditorSupport{

    private KeyDomain keyDomain;

    /**
     * example : 10435
     * - 10 will be keyId
     * - 435 will be baseOfficeId
     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException{
        KeyDomain keyDomain = new KeyDomain();
        keyDomain.setKeyId(Integer.parseInt(text.substring(0,1)));
        keyDomain.setBaseOfficeId(Integer.parseInt(text.substring(2,4)));       
        setValue(keyDomain);
    }

    @Override
    public String getAsText() {
        KeyDomain value = (KeyDomain) getValue();
        return (value != null ? value.toString() : "");
    }

    public void setKeyDomain(KeyDomain keyDomain) {
        this.keyDomain = keyDomain;
    }   
}

我认为我可以使用属性编辑器将我的URI字符串变为适当的对象类型。我已经进行了实现并配置CustomEditorConfigurer,但是我总是得到ConversionNotsupportedException。

如果我在控制器上添加initbinder,一切都会好起来的:

@InitBinder
public void setBinder(WebDataBinder dataBinder) {
    dataBinder.registerCustomEditor(KeyDomain.class, new KeyPropertyEditor());      
}

我得到这样的警告

warn:org.springframework.beans.factory.config.customeditorconfigurer-将propertyEditor实例传递到customeditorconfigurer中:使用propertyeditoregrigrars或PropertyEditor类名称。犯有键[com.template.baseline.domain.keydomain;违规编辑器实例:com.template.baseline.propertyeditor.keypropertyeditor@1a271f5

感谢你的回答。

PS:注入AnnotationMethodHandlerAdlerAdapter上

<beans:bean id="AnnotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <beans:property name="webBindingInitializer">
        <beans:bean class="com.template.baseline.initialize.CustomWebBindingInitializer" />
    </beans:property>
</beans:bean>

和实施

public class CustomWebBindingInitializer implements WebBindingInitializer  {

public CustomWebBindingInitializer(){
    System.out.println("******** constructor *********");
}

public void initBinder(WebDataBinder binder, WebRequest request) {
    System.out.println("******** initBinder *********");
    binder.registerCustomEditor(KeyDomain.class, new KeyDomainPropertyEditor());
}

}

有帮助吗?

解决方案

CustomEditorConfigurer 与Web请求数据绑定无关。

如果您想注册您的 PropertyEditor 全球,您需要实施 WebBindingInitializer 和供应 AnnotationMethodHandlerAdapter 用它:

<bean 
    class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <proeprty name = "webBindingInitializer">
        <bean class = "MyWebBindingInitializer" />
    </property>
</bean>

另一个选择是将您的转换逻辑实现为 Formatter 并通过 FormattingConversionServiceFactoryBean<mvc:annotation-driven>, , 看 MVC-Showcase样本.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top