SpringMVC中日期格式的轉(zhuǎn)換
解決日期提交轉(zhuǎn)換異常的問(wèn)題
由于日期數(shù)據(jù)有很多種格式,所以springmvc沒(méi)辦法把字符串轉(zhuǎn)換成日期類型。所以需要自定義參數(shù)綁定。前端控制器接收到請(qǐng)求后,找到注解形式的處理器適配器,對(duì)RequestMapping標(biāo)記的方法進(jìn)行適配,并對(duì)方法中的形參進(jìn)行參數(shù)綁定。在springmvc這可以在處理器適配器上自定義Converter進(jìn)行參數(shù)綁定。如果使用<mvc:annotation-driven/>可以在此標(biāo)簽上進(jìn)行擴(kuò)展。
1.自定義DataConvertor類, 并實(shí)現(xiàn)Convertor接口
public class DateConverter implements Converter<String, Date> { @Override public Date convert(String source) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return simpleDateFormat.parse(source); } catch (ParseException e) { e.printStackTrace(); } return null; } }
2.在springmvc.xml配置文件中注冊(cè)轉(zhuǎn)換器
方法一:通過(guò)注解驅(qū)動(dòng)的方式加載轉(zhuǎn)換器
<!-- 配置mvc注解驅(qū)動(dòng) --> <mvc:annotation-driven conversion-service="conversionService"/> <!-- 配置日期轉(zhuǎn)換器 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.rodge.ssm.converter.DateConverter"></bean> </set> </property> </bean>
方法二:通過(guò)自定義webBinder配置(不常用)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 掃描帶Controller注解的類 --> <context:component-scan base-package="cn.itcast.springmvc.controller" /> <!-- 轉(zhuǎn)換器配置 --> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.itcast.springmvc.convert.DateConverter"/> </set> </property> </bean> <!-- 自定義webBinder --> <bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService" /> </bean> <!--注解適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer" ref="customBinder"></property> </bean> <!-- 注解處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 加載注解驅(qū)動(dòng) --> <!-- <mvc:annotation-driven/> --> <!-- 視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- jsp前綴 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- jsp后綴 --> <property name="suffix" value=".jsp" /> </bean> </beans>
注意:此方法需要獨(dú)立配置處理器映射器、適配器,不再使用<mvc:annotation-driven/>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)人員信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)人員信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼
這篇文章主要介紹了Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11詳解Java使用Pipeline對(duì)Redis批量讀寫(hmset&hgetall)
本篇文章主要介紹了Java使用Pipeline對(duì)Redis批量讀寫(hmset&hgetall),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12詳解json在SpringBoot中的格式轉(zhuǎn)換
這篇文章主要介紹了詳解json在SpringBoot中的格式轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題
這篇文章主要介紹了關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08springboot?整合?clickhouse的實(shí)現(xiàn)示例
本文主要介紹了springboot?整合?clickhouse的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02SpringBoot核心@SpringBootApplication使用介紹
這篇文章主要介紹了SpringBoot核心@SpringBootApplication的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03