SpringMVC自定義類型轉(zhuǎn)換器實(shí)現(xiàn)解析
這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
頁面錄入的字符串:2019/12/05可以映射到實(shí)體的日期屬性上,但是如果是錄入2019-12-05就會(huì)報(bào)錯(cuò)400 bad request,想要以2019-12-05日期格式的方式映射到實(shí)體的日期屬性上,需要自定義類型轉(zhuǎn)換器,主要步驟如下:
1、 自定義類實(shí)現(xiàn)Convertro<S,T>接口
2、Springmvc.xml中配置ConversionServiceFactoryBean,其屬性上配置我們自定義的轉(zhuǎn)換器
3、欲使配置的轉(zhuǎn)換器生效,需要將springmvc.xml的<mvc:annotation-driven />改為
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
1、 自定義類實(shí)現(xiàn)Convertro<S,T>接口
package com.example.util;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StingToDateConvertr implements Converter<String, Date> {
@Override
public Date convert(String s) {
if(StringUtils.isEmpty(s)){
throw new RuntimeException("日期字符串不能為空!");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(s);
} catch (ParseException e) {
throw new RuntimeException("類型轉(zhuǎn)換出錯(cuò)!");
}
}
}
2、Springmvc.xml中配置ConversionServiceFactoryBean,其屬性上配置我們自定義的轉(zhuǎn)換器
<!--配置自定義類型轉(zhuǎn)換器--> <bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.example.util.StingToDateConvertr" /> </set> </property> </bean>
3、欲使配置的轉(zhuǎn)換器生效,需要將springmvc.xml的<mvc:annotation-driven />改為
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
springmvc.xml的完整配置如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<!--開啟注解掃描-->
<context:component-scan base-package="com.example" />
<!--視圖解析器,根據(jù)Controller返回的字符串找對應(yīng)的文件-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--文件路徑-->
<property name="prefix" value="/WEB-INF/pages/" />
<!--文件后綴-->
<property name="suffix" value=".jsp" />
</bean>
<!--配置自定義類型轉(zhuǎn)換器-->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.example.util.StingToDateConvertr" />
</set>
</property>
</bean>
<!--1、開啟springmvc框架注解的支持-->
<!--2、欲使配置的自定義類型轉(zhuǎn)換器生效,需加上conversion-service屬性-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
</beans>
注意:自定義的類型轉(zhuǎn)換器生效之后,日期格式就只能使用yyyy-MM-dd的格式了,若再使用原有的yyyy/MM/dd格式就會(huì)報(bào)錯(cuò)!
如有理解不到之處,望指正!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java面向?qū)ο笤O(shè)計(jì)原則之合成復(fù)用原則示例詳解
這篇文章主要介紹了java面向?qū)ο笤O(shè)計(jì)原則之合成復(fù)用原則的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-10-10
SpringBoot集成內(nèi)存數(shù)據(jù)庫Sqlite的實(shí)踐
sqlite這樣的內(nèi)存數(shù)據(jù)庫,小巧可愛,做小型服務(wù)端演示程序,非常好用,本文主要介紹了SpringBoot集成Sqlite,具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09
Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
使用Spring掃描Mybatis的mapper接口的三種配置
這篇文章主要介紹了使用Spring掃描Mybatis的mapper接口的三種配置,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot打印Banner的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot啟動(dòng)Banner的實(shí)現(xiàn)原理和打印流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
如何使用Spring Security手動(dòng)驗(yàn)證用戶的方法示例
這篇文章主要介紹了如何使用Spring Security手動(dòng)驗(yàn)證用戶的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05

