SpringMVC中日期格式的轉(zhuǎn)換
解決日期提交轉(zhuǎn)換異常的問題
由于日期數(shù)據(jù)有很多種格式,所以springmvc沒辦法把字符串轉(zhuǎn)換成日期類型。所以需要自定義參數(shù)綁定。前端控制器接收到請求后,找到注解形式的處理器適配器,對RequestMapping標(biāo)記的方法進行適配,并對方法中的形參進行參數(shù)綁定。在springmvc這可以在處理器適配器上自定義Converter進行參數(shù)綁定。如果使用<mvc:annotation-driven/>可以在此標(biāo)簽上進行擴展。
1.自定義DataConvertor類, 并實現(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配置文件中注冊轉(zhuǎn)換器
方法一:通過注解驅(qū)動的方式加載轉(zhuǎn)換器
<!-- 配置mvc注解驅(qū)動 -->
<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>
方法二:通過自定義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ū)動 -->
<!-- <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>
注意:此方法需要獨立配置處理器映射器、適配器,不再使用<mvc:annotation-driven/>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
聊聊Spring MVC JSON數(shù)據(jù)交互的問題
我們在開發(fā)中后端經(jīng)常需要接受來自于前端傳遞的Json字符串?dāng)?shù)據(jù),怎么把Json字符串轉(zhuǎn)換為Java對象呢?下面小編給大家?guī)砹薙pring MVC JSON數(shù)據(jù)交互的問題,感興趣的朋友一起看看吧2021-10-10
springboot如何通過不同的策略動態(tài)調(diào)用不同的實現(xiàn)類
這篇文章主要介紹了springboot如何通過不同的策略動態(tài)調(diào)用不同的實現(xiàn)類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring Security中successHandler無效問題及解決
這篇文章主要介紹了Spring Security中successHandler無效問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Java實戰(zhàn)寵物店在線交易平臺的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+maven+Mysql+FreeMarker實現(xiàn)一個寵物在線交易系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01
Mybatisplus實現(xiàn)JSON處理器的示例代碼
Mybatisplusjson是基于Mybatisplus開發(fā)的一個json工具庫,本文主要介紹了Mybatisplus實現(xiàn)JSON處理器的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-03-03

