本篇文章為大家展示了SpringMVC中怎么實(shí)現(xiàn)一個(gè)自定義類型轉(zhuǎn)換器,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都地區(qū)優(yōu)秀IDC服務(wù)器托管提供商(創(chuàng)新互聯(lián)).為客戶提供專業(yè)的資陽(yáng)托管服務(wù)器,四川各地服務(wù)器托管,資陽(yáng)托管服務(wù)器、多線服務(wù)器托管.托管咨詢專線:13518219792
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返回的字符串找對(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> <!--配置自定義類型轉(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)容就是SpringMVC中怎么實(shí)現(xiàn)一個(gè)自定義類型轉(zhuǎn)換器,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:SpringMVC中怎么實(shí)現(xiàn)一個(gè)自定義類型轉(zhuǎn)換器
分享地址:http://aaarwkj.com/article6/peghog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、定制網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站營(yíng)銷、網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司
聲明:本網(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)