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

发布于 2024-10-05 00:27:11 字数 3704 浏览 2 评论 0原文

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

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

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

并且我收到类似这样的警告

警告:org.springframework.beans.factory.config.CustomEditorConfigurer - 不推荐将 PropertyEditor 实例传递到 CustomEditorConfigurer 中:改用 PropertyEditorRegistrars 或 PropertyEditor 类名。违规密钥 [com.template.baseline.domain.KeyDomain;有问题的编辑器实例:com.template.baseline.propertyEditor.KeyPropertyEditor@1a271f5

感谢您的回答。

ps:webBindingInitalizer 注入 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());
}

}

i just wonder, can i convert uri string to another object type ?

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

and here my configuration

<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>

and propertyEditor Class :

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;
    }   
}

i thought that i can use Property Editor to convert my URI string become appropriate object type. i already made an implementation and configure CustomEditorConfigurer, but i always get ConversionNotSupportedException.

if i add initBinder at my controller, everything will just fine :

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

and i get Warning something like this

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

thanks for the answer.

ps : webBindingInitalizer injected on 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>

and Implementation

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());
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-10-12 00:27:11

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-showcase 示例

CustomEditorConfigurer has nothing to do with web request data binding.

If you want to register your PropertyEditor globablly, you need to implement WebBindingInitializer and supply AnnotationMethodHandlerAdapter with it:

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

Another option is to implement your conversion logic as a Formatter and configure it via FormattingConversionServiceFactoryBean and <mvc:annotation-driven>, see mvc-showcase sample.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文