كيفية تحويل سلسلة URI إلى نوع الكائن المعقد في الربيع

StackOverflow https://stackoverflow.com/questions/4296747

سؤال

أنا فقط أتساءل ، هل يمكنني تحويل سلسلة 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 إلى CustomItoritorConfigurer: استخدام propertyeditorregistrars أو أسماء فئة الممتلكات بدلاً من ذلك. مفتاح الإساءة [com.template.baseline.domain.keydomain ؛ مثيل محرر الإساءة: com.template.baseline.propertyeitor.keypropertyeditor@1a271f5

شكرا على الاجابة.

ملاحظة: تم حقن webbindinginitalizer على التعليق التوضيحي methodhandleradapter

<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 لا علاقة له بربط بيانات طلب الويب.

إذا كنت تريد تسجيل الخاص بك PropertyEditor Globablly ، تحتاج إلى تنفيذ 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