免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

SpringMVC中怎么實(shí)現(xiàn)一個自定義類型轉(zhuǎn)換器-創(chuàng)新互聯(lián)

本篇文章為大家展示了SpringMVC中怎么實(shí)現(xiàn)一個自定義類型轉(zhuǎn)換器,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為婁煩等服務(wù)建站,婁煩等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為婁煩企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

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)換出錯!");  } }}

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="/tupian/20230522/"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:aop="/tupian/20230522/"  xmlns:c="http://www.springframework.org/schema/c"  xmlns:cache="/tupian/20230522/"  xmlns:context="/tupian/20230522/"  xmlns:jdbc="/tupian/20230522/"  xmlns:jee="/tupian/20230522/"  xmlns:lang="/tupian/20230522/"  xmlns:mvc="/tupian/20230522/"  xmlns:p="http://www.springframework.org/schema/p"  xmlns:task="/tupian/20230522/"  xmlns:tx="/tupian/20230522/"  xmlns:util="/tupian/20230522/"  xsi:schemaLocation="/tupian/20230522/ /tupian/20230522//spring-jee-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-mvc-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-context-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-aop-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-util-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-jdbc-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-cache-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-task-4.3.xsd  /tupian/20230522/ /tupian/20230522//spring-beans.xsd  /tupian/20230522/ /tupian/20230522//spring-lang-4.3.xsd  /tupian/20230522/ /tupian/20230522//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)容就是SpringMVC中怎么實(shí)現(xiàn)一個自定義類型轉(zhuǎn)換器,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁題目:SpringMVC中怎么實(shí)現(xiàn)一個自定義類型轉(zhuǎn)換器-創(chuàng)新互聯(lián)
標(biāo)題URL:http://m.newbst.com/article28/hjscp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)網(wǎng)站改版網(wǎng)站制作網(wǎng)站導(dǎo)航定制網(wǎng)站網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司