SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析
這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
頁面錄入的字符串:2019/12/05可以映射到實體的日期屬性上,但是如果是錄入2019-12-05就會報錯400 bad request,想要以2019-12-05日期格式的方式映射到實體的日期屬性上,需要自定義類型轉(zhuǎn)換器,主要步驟如下:
1、 自定義類實現(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、 自定義類實現(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)換出錯!");
}
}
}
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格式就會報錯!
如有理解不到之處,望指正!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java線性表的存儲結(jié)構(gòu)及其代碼實現(xiàn)
這篇文章主要為大家詳細介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第一篇,線性表的存儲結(jié)構(gòu)及其代碼實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
Intellij IDEA解析jacoco結(jié)果文件的方法
這篇文章主要介紹了Intellij IDEA解析jacoco結(jié)果文件的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Java數(shù)據(jù)結(jié)構(gòu)及算法實例:選擇排序 Selection Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:選擇排序 Selection Sort,本文直接給出實現(xiàn)代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06
springboot下mybatis-plus如何打印sql日志和參數(shù)到日志文件
本文主要介紹了springboot下mybatis-plus如何打印sql日志和參數(shù)到日志文件,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

