中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

使用SpringMVC怎么自定義一個類型轉(zhuǎn)換器-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)使用SpringMVC怎么自定義一個類型轉(zhuǎn)換器,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供平川網(wǎng)站建設(shè)、平川做網(wǎng)站、平川網(wǎng)站設(shè)計、平川網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、平川企業(yè)網(wǎng)站模板建站服務(wù),10多年平川做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

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>

關(guān)于使用SpringMVC怎么自定義一個類型轉(zhuǎn)換器就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.2m8n56k.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文名稱:使用SpringMVC怎么自定義一個類型轉(zhuǎn)換器-創(chuàng)新互聯(lián)
本文路徑:http://www.2m8n56k.cn/article30/dshoso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號App設(shè)計網(wǎng)站維護ChatGPT網(wǎng)站制作建站公司

廣告

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

手機網(wǎng)站建設(shè)
主站蜘蛛池模板: 国产孕妇孕交大片孕 | 国产精品免费综合一区视频 | 国产视频99 | 三级黄色a | 日本色网址 | 欧美国产日韩一区二区三区 | 精品一区二区三区视频在线观看 | 亚洲欧美日韩国产综合 | 欧美一级黄色毛片 | 久久亚洲国产最新网站 | 艳女伦交一级毛片 | 18视频网站在线观看 | 日本欧美韩国一区二区三区 | 国产精品一区二区四区 | 国产不卡精品一区二区三区 | 精品视自拍视频在线观看 | 国产第九页 | 久久一区二区三区免费播放 | 久草资源福利 | 黄色aaa毛片 | 在线免费看一级片 | 欧美日韩视频在线第一区二区三区 | aaaa毛片 | 中文国产成人精品久久一区 | 91九九 | 男女性高清爱潮视频免费观看 | 成人国产片免费 | 国产精品成人自拍 | 久久久久琪琪精品色 | 亚洲日本高清成人aⅴ片 | 日韩美三级 | 真正全免费视频a毛片 | 欧美亚洲一区二区三区 | 日韩欧美色综合 | 久久五月女厕所一区二区 | 日韩亚洲一区二区三区 | 国产精品观看在线亚洲人成网 | 欧美色黄毛片 | 国产亚洲福利精品一区二区 | 亚洲男人天堂手机版 | 亚洲人成网站在线在线 |