java - spring中如何将uri字符串转换为复杂对象类型

标签 java spring uri propertyeditor

我只是想知道,我可以将 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>

和 propertyEditor 类:

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。

如果我在 Controller 中添加 initBinder,一切都会很好:

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

我收到类似这样的警告

WARN : org.springframework.beans.factory.config.CustomEditorConfigurer - Passing PropertyEditor instances into CustomEditorConfigurer is deprecated: use PropertyEditorRegistrars or PropertyEditor class names instead. Offending key [com.template.baseline.domain.KeyDomain; offending editor instance: com.template.baseline.propertyEditor.KeyPropertyEditor@1a271f5

感谢您的回答。

ps:webBindingInitalizer 注入(inject) AnnotationMethodHandlerAdapter

<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 请求数据绑定(bind)无关。

如果您想注册您的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 sample .

关于java - spring中如何将uri字符串转换为复杂对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4296747/

相关文章:

java - Spring数据查询有条件吗?

android - 使用路径、路径前缀或路径模式的 Intent 过滤器

java - 如何将Frame(jframe)集成到jsp中

java - 在 GWT 中连接数据库并出现奇怪的错误

java - 如何使用 log4j 关闭注销?

java - Spring:在 Maven 插件项目中请求加载属性,但使用插件所在项目中的文件

java - 服务器仅读取从客户端发送的第一个对象

java - Maven/Spring 私有(private)存储库设置

java - 安卓 5.1 : can not capture and get full-size image

android - 使用 SUGGEST_COLUMN_ICON_1 的网址作为搜索建议