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)文章
聊聊Spring MVC JSON數(shù)據(jù)交互的問(wèn)題
我們?cè)陂_(kāi)發(fā)中后端經(jīng)常需要接受來(lái)自于前端傳遞的Json字符串?dāng)?shù)據(jù),怎么把Json字符串轉(zhuǎn)換為Java對(duì)象呢?下面小編給大家?guī)?lái)了Spring MVC JSON數(shù)據(jù)交互的問(wèn)題,感興趣的朋友一起看看吧2021-10-10
springboot如何通過(guò)不同的策略動(dòng)態(tài)調(diào)用不同的實(shí)現(xiàn)類
這篇文章主要介紹了springboot如何通過(guò)不同的策略動(dòng)態(tài)調(diào)用不同的實(shí)現(xiàn)類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
解決cmd運(yùn)行java程序“找不到文件”提示的方案
在本篇文章里小編給大家分享的是關(guān)于解決cmd運(yùn)行java程序“找不到文件”提示的方案,有需要的朋友們可以參考下。2020-02-02
SpringBoot整合阿里云視頻點(diǎn)播的過(guò)程詳解
視頻點(diǎn)播(ApsaraVideo for VoD)是集音視頻采集、編輯、上傳、自動(dòng)化轉(zhuǎn)碼處理、媒體資源管理、分發(fā)加速于一體的一站式音視頻點(diǎn)播解決方案。這篇文章主要介紹了SpringBoot整合阿里云視頻點(diǎn)播的詳細(xì)過(guò)程,需要的朋友可以參考下2021-12-12
Spring Security中successHandler無(wú)效問(wèn)題及解決
這篇文章主要介紹了Spring Security中successHandler無(wú)效問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java實(shí)戰(zhàn)寵物店在線交易平臺(tái)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+maven+Mysql+FreeMarker實(shí)現(xiàn)一個(gè)寵物在線交易系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2022-01-01
Mybatisplus實(shí)現(xiàn)JSON處理器的示例代碼
Mybatisplusjson是基于Mybatisplus開(kāi)發(fā)的一個(gè)json工具庫(kù),本文主要介紹了Mybatisplus實(shí)現(xiàn)JSON處理器的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
SpringBoot集成kafka全面實(shí)戰(zhàn)記錄
在實(shí)際開(kāi)發(fā)中,我們可能有這樣的需求,應(yīng)用A從TopicA獲取到消息,經(jīng)過(guò)處理后轉(zhuǎn)發(fā)到TopicB,再由應(yīng)用B監(jiān)聽(tīng)處理消息,即一個(gè)應(yīng)用處理完成后將該消息轉(zhuǎn)發(fā)至其他應(yīng)用,完成消息的轉(zhuǎn)發(fā),這篇文章主要介紹了SpringBoot集成kafka全面實(shí)戰(zhàn),需要的朋友可以參考下2021-11-11

