1

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:AnnotationMethodHandlerAdapterに挿入されたwebBindingInitalizer

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

}

4

1 に答える 1

1

CustomEditorConfigurerWebリクエストのデータバインディングとは何の関係もありません。

PropertyEditorglobabllyを登録したい場合は、それを実装WebBindingInitializerして提供する必要がありますAnnotationMethodHandlerAdapter

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

もう1つのオプションは、変換ロジックをとして実装し、およびFormatterを介して構成することです。mvc-showcaseサンプルを参照してください。FormattingConversionServiceFactoryBean<mvc:annotation-driven>

于 2010-11-28T13:57:54.147 に答える