這篇文章將為大家詳細(xì)講解有關(guān)使用SpringMVC怎么自定義一個(gè)類(lèi)型轉(zhuǎn)換器,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供平川網(wǎng)站建設(shè)、平川做網(wǎng)站、平川網(wǎng)站設(shè)計(jì)、平川網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、平川企業(yè)網(wǎng)站模板建站服務(wù),10多年平川做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。1、 自定義類(lèi)實(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、 自定義類(lèi)實(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("類(lèi)型轉(zhuǎn)換出錯(cuò)!"); } } }
2、Springmvc.xml中配置ConversionServiceFactoryBean,其屬性上配置我們自定義的轉(zhuǎn)換器
<!--配置自定義類(lèi)型轉(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 "> <!--開(kāi)啟注解掃描--> <context:component-scan base-package="com.example" /> <!--視圖解析器,根據(jù)Controller返回的字符串找對(duì)應(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> <!--配置自定義類(lèi)型轉(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、開(kāi)啟springmvc框架注解的支持--> <!--2、欲使配置的自定義類(lèi)型轉(zhuǎn)換器生效,需加上conversion-service屬性--> <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/> </beans>
關(guān)于使用SpringMVC怎么自定義一個(gè)類(lèi)型轉(zhuǎn)換器就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站aaarwkj.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文名稱:使用SpringMVC怎么自定義一個(gè)類(lèi)型轉(zhuǎn)換器-創(chuàng)新互聯(lián)
本文路徑:http://aaarwkj.com/article30/dshoso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、App設(shè)計(jì)、網(wǎng)站維護(hù)、ChatGPT、網(wǎng)站制作、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容