spring mvc 參數類型轉換

實現方式以字符串轉Date爲例說明:web

全局配置

第一種:實現 Converter 接口

  • 實現類: 
    public class StringToDateConveter implements Converter {spring

    private String formatPatten;
    
        public StringToDateConveter(String formatPatten){
            this.formatPatten=formatPatten;
        }
    
        @Override
        public Date convert(String s) {
            return DateUtil.string2Date(s,formatPatten);
        }
    }
  • mvc.xml配置api

    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.lannong.api.www.converter.StringToDateConveter">
                    <constructor-arg name="formatPatten" value="yyyy-MM-dd"/>
                </bean>
            </set>
        </property>
    </bean>
  • 配置到handlerAdaptermvc

    <!--使用 ConfigurableWebBindingInitializer 註冊conversionService-->
       <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService"/>
       </bean>
    
       <!-- 註冊ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter-->
       <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="webBindingInitializer" ref="webBindingInitializer"/>
       </bean>

第二種:實現Formatter接口,與第一種實現方式相似

  • 實現類app

    public class MyDateFormater implements Formatter<Date> {
    
        @Override
        public Date parse(String s, Locale locale) throws ParseException {
            return DateTimeUtil.string2Date(s,"yyyy-MM-dd");
        }
    
        @Override
        public String print(Date date, Locale locale) {
            return null;
        }
    }
  • mvc.xml配置ide

    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="formatters">
            <set>
                <bean class="com.lannong.api.www.converter.MyDateFormater"/>
            </set>
        </property>
    </bean>

第三種:實現WebBindingInitializer接口

  • 實現類this

    public class MyWebBindingInitializer implements WebBindingInitializer {
    
        @Override
        public void initBinder(WebDataBinder binder, WebRequest request) {
    
            binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
                @Override
                public void setAsText(String text) {
                    setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
                }
            });
        }
    }
  • mvc.xml配置code

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="webBindingInitializer">
            <bean class="com.lannong.api.www.binder.MyWebBindingInitializer"/>
        </property>
        <!-- others config -->
    </bean>

局部配置

在Controller中添加轉換方法並添加@InitBinder

  • 代碼orm

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        simpleDateFormat.setLenient(false);
        webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
    }
    
    或
    
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateTimeUtil.string2Date(text, "yyyy-MM-dd"));
            }
        });
    }

兩種方式均可以,做用域和該方法做用域同樣xml

使用@DateTimeFormat(pattern = "yyyy-MM-dd")

註解能夠加在屬性上,也能夠加在方法上,須要導入joda-time.jar。另外日期參數的格式須要和patten定義的一致,不然會報400錯誤

相關文章
相關標籤/搜索