SpringMVC繫結引數之型別轉換有三種方式:
1. 實體類中加日期格式化註解
@DateTimeFormat(pattern="yyyy-MM-dd hh:MM") private Date creationTime;2.在Controller類的方法中加入一段程式碼
/** * 在controller層中加入一段資料繫結程式碼 * @param webDataBinder */ @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)); }備註:自定義型別轉換器必須實現PropertyEditor介面或者繼承PropertyEditorSupport類
寫一個類 extends propertyEditorSupport(implements PropertyEditor){ public void setAsText(String text){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy -MM-dd hh:mm"); Date date = simpleDateFormat.parse(text); this.setValue(date); } public String getAsTest(){ Date date = (Date)this.getValue(); return this.dateFormat.format(date); } }3.(spring 3.0以前使用正常,以後的版本需要使用< mvc:annotation-driven/>註冊使用)使用xml配置實現型別轉換(系統全域性轉換器)
1.註冊conversionservice
<!-- 註冊ConversionService--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.ezubo.global.portal.util.StringToDateConverter"> <constructor-arg index="0" value="yyyy-MM-dd hh:mm"/> </bean> </set> </property> </bean>備註:StringtoDateConverter.java的實現:
/** * Created by 蘇 on 15-10-13. */ public class StringToDateConverter implements Converter<String,Date> { private static final Logger logger = LoggerFactory.getLogger(StringToDateConverter.class); private String pattern; public StringToDateConverter(String pattern){ this.pattern = pattern; } public Date convert(String s) { if(StringUtils.isBlank(s)){ return null; } SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); simpleDateFormat.setLenient(false); try{ return simpleDateFormat.parse(s); }catch(ParseException e){ logger.error("轉換日期異常:"+e.getMessage() , e); throw new IllegalArgumentException("轉換日期異常:"+e.getMessage() , e); } } }2.使用 ConfigurableWebBindingInitializer 註冊conversionService
<!--使用 ConfigurableWebBindingInitializer 註冊conversionService--> <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService"/> </bean>3.註冊ConfigurableWebBindingInitializer到RequestMappingHandlerAdapter
<!-- 註冊ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer" ref="webBindingInitializer"/> <!-- 執行緒安全的訪問session--> <property name="synchronizeOnSession" value="true"/> </bean>
此時可能有人會問,如果我同時使用 PropertyEditor 和 ConversionService,執行順序是什麼呢?內部首先查詢PropertyEditor 進行型別轉換,如果沒有找到相應的 PropertyEditor 再通過 ConversionService進行轉換。
4.(spring 3.2以後使用正常)使用<mvc:annotation-driven/>註冊conversionService
具體原因請參考:
SpringMVC配置全域性日期轉換器,處理日期轉換異常
1.註冊ConversionService
<!-- 註冊ConversionService--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.ezubo.global.portal.util.StringToDateConverter"> <constructor-arg index="0" value="yyyy-MM-dd hh:mm"/> </bean> </set> </property> </bean>
2. 需要修改springmvc-servlet xml配置檔案中的annotation-driven,增加屬性conversion-service指向新增的 conversionService。
<mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/> <!--轉換時設定特性--> <property name="features"> <array> <!--避免預設的迴圈引用替換--> <ref bean="DisableCircularReferenceDetect"/> <ref bean="WriteMapNullValue"/> <ref bean="WriteNullStringAsEmpty"/> <ref bean="WriteNullNumberAsZero"/> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>