SpringMVC 傳日期參數(shù)到后臺(tái)的實(shí)例講解
1、注解方式,在controller層通過initBinder注解實(shí)現(xiàn)
@InitBinder
public void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)throws Exception {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);
binder.registerCustomEditor(Date.class, dateEditor);
}
2、類型轉(zhuǎn)換,SpringMvc提供了Converter接口
public class DateConvert implements Converter<String, Date> {
@Override
public Date convert(String stringDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
return simpleDateFormat.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
spring.xml中配置轉(zhuǎn)換器
<!-- 第一步: 創(chuàng)建自定義日期轉(zhuǎn)換規(guī)則 --> <bean id="dateConvert" class="xxx.xxx.DateConvert"/> <!-- 第二步: 創(chuàng)建convertion-Service ,并注入dateConvert--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <ref bean="dateConvert"/> </set> </property> </bean> <!-- 第三步:注冊(cè)處理器映射器/處理器適配器 ,添加conversion-service屬性--> <mvc:annotation-driven conversion-service="conversionService"/>
以上這篇SpringMVC 傳日期參數(shù)到后臺(tái)的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot通過url訪問本地圖片代碼實(shí)例
這篇文章主要介紹了springboot通過url訪問本地圖片代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
lambda表達(dá)式解決java后臺(tái)分組排序過程解析
這篇文章主要介紹了lambda表達(dá)式解決java后臺(tái)分組排序過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
maven中springboot-maven-plugin的5種打包方式
本文主要介紹了maven中springboot-maven-plugin的5種打包方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法
本篇文章主要介紹了Spring Boot多數(shù)據(jù)源及其事務(wù)管理配置方法,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04
Java把多個(gè)對(duì)象的list的數(shù)據(jù)合并的方法示例
在Java中合并多個(gè)List可以使用遍歷、Stream API或Apache Commons Collections,通過遍歷各個(gè)List并將元素添加到新List實(shí)現(xiàn)合并,Java 8提供了Stream API,使用Stream.of()和flatMap()可以簡(jiǎn)潔地合并List,文中給出了多種方法示例,需要的朋友可以參考下2024-09-09

