最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC自定義類型轉(zhuǎn)換器實(shí)現(xiàn)解析

 更新時(shí)間:2019年12月05日 14:54:01   作者:1572662  
這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了SpringMVC自定義類型轉(zhuǎn)換器實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

頁(yè)面錄入的字符串:2019/12/05可以映射到實(shí)體的日期屬性上,但是如果是錄入2019-12-05就會(huì)報(bào)錯(cuò)400 bad request,想要以2019-12-05日期格式的方式映射到實(shí)體的日期屬性上,需要自定義類型轉(zhuǎn)換器,主要步驟如下:

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 ">
 <!--開(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>
 <!--配置自定義類型轉(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、欲使配置的自定義類型轉(zhuǎn)換器生效,需加上conversion-service屬性-->
 <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>

</beans>

注意:自定義的類型轉(zhuǎn)換器生效之后,日期格式就只能使用yyyy-MM-dd的格式了,若再使用原有的yyyy/MM/dd格式就會(huì)報(bào)錯(cuò)!

如有理解不到之處,望指正!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java筆記學(xué)習(xí)之操作符

    java筆記學(xué)習(xí)之操作符

    本篇文章給大家分享了關(guān)于Java中操作符的相關(guān)知識(shí)點(diǎn)以及難點(diǎn)總結(jié),有需要的朋友參考學(xué)習(xí)下吧。
    2018-04-04
  • java面向?qū)ο笤O(shè)計(jì)原則之合成復(fù)用原則示例詳解

    java面向?qū)ο笤O(shè)計(jì)原則之合成復(fù)用原則示例詳解

    這篇文章主要介紹了java面向?qū)ο笤O(shè)計(jì)原則之合成復(fù)用原則的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-10-10
  • SpringBoot集成內(nèi)存數(shù)據(jù)庫(kù)Sqlite的實(shí)踐

    SpringBoot集成內(nèi)存數(shù)據(jù)庫(kù)Sqlite的實(shí)踐

    sqlite這樣的內(nèi)存數(shù)據(jù)庫(kù),小巧可愛(ài),做小型服務(wù)端演示程序,非常好用,本文主要介紹了SpringBoot集成Sqlite,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-09-09
  • Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Java實(shí)現(xiàn)下載文件的6種方式

    Java實(shí)現(xiàn)下載文件的6種方式

    本文主要介紹了Java實(shí)現(xiàn)下載文件的6種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java仿淘寶首頁(yè)分類列表功能的示例代碼

    Java仿淘寶首頁(yè)分類列表功能的示例代碼

    這篇文章主要介紹了仿淘寶分類管理功能的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,也給大家做個(gè)參考
    2018-05-05
  • 一文詳解如何排查定位Java中的死鎖

    一文詳解如何排查定位Java中的死鎖

    在當(dāng)今數(shù)字化時(shí)代,微服務(wù)架構(gòu)憑借其高可擴(kuò)展性、靈活性和易于維護(hù)等優(yōu)勢(shì),成為了眾多企業(yè)構(gòu)建大型應(yīng)用系統(tǒng)的首選架構(gòu)模式,當(dāng)我們將微服務(wù)部署在 Linux 服務(wù)器上時(shí),有時(shí)會(huì)遭遇令人頭疼的死鎖問(wèn)題,本位給大家介紹了如何排查定位Java中的死鎖,需要的朋友可以參考下
    2025-02-02
  • 使用Spring掃描Mybatis的mapper接口的三種配置

    使用Spring掃描Mybatis的mapper接口的三種配置

    這篇文章主要介紹了使用Spring掃描Mybatis的mapper接口的三種配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot打印Banner的實(shí)現(xiàn)示例

    SpringBoot打印Banner的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot啟動(dòng)Banner的實(shí)現(xiàn)原理和打印流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • 如何使用Spring Security手動(dòng)驗(yàn)證用戶的方法示例

    如何使用Spring Security手動(dòng)驗(yàn)證用戶的方法示例

    這篇文章主要介紹了如何使用Spring Security手動(dòng)驗(yàn)證用戶的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-05-05

最新評(píng)論

禄丰县| 吴桥县| 张家口市| 调兵山市| 荃湾区| 建昌县| 秦皇岛市| 望城县| 青龙| 贵港市| 汉川市| 会东县| 邢台市| 禹城市| 仁化县| 文登市| 武义县| 洞口县| 阳谷县| 敦化市| 卓资县| 青州市| 望奎县| 正蓝旗| 衡南县| 钦州市| 达拉特旗| 刚察县| 康定县| 泰兴市| 桦川县| 青州市| 安多县| 岳阳县| 庆城县| 万山特区| 宜宾市| 鹿泉市| 芦山县| 济南市| 醴陵市|