亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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 ">
 <!--開啟注解掃描-->
 <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ò)!

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

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

相關(guān)文章

  • java線性表的存儲(chǔ)結(jié)構(gòu)及其代碼實(shí)現(xiàn)

    java線性表的存儲(chǔ)結(jié)構(gòu)及其代碼實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第一篇,線性表的存儲(chǔ)結(jié)構(gòu)及其代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Intellij IDEA解析jacoco結(jié)果文件的方法

    Intellij IDEA解析jacoco結(jié)果文件的方法

    這篇文章主要介紹了Intellij IDEA解析jacoco結(jié)果文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 使用webmagic實(shí)現(xiàn)爬蟲程序示例分享

    使用webmagic實(shí)現(xiàn)爬蟲程序示例分享

    這篇文章主要介紹了使用webmagic實(shí)現(xiàn)爬蟲程序示例,需要的朋友可以參考下
    2014-04-04
  • Java AES加密和解密教程

    Java AES加密和解密教程

    這篇文章主要介紹了Java AES加密和解密的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-12-12
  • Java?GUI實(shí)現(xiàn)多個(gè)窗口切換效果

    Java?GUI實(shí)現(xiàn)多個(gè)窗口切換效果

    這篇文章主要為大家詳細(xì)介紹了Java?GUI實(shí)現(xiàn)多個(gè)窗口的切換效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java全面細(xì)致講解final的使用

    Java全面細(xì)致講解final的使用

    關(guān)于final關(guān)鍵字,它也是我們一個(gè)經(jīng)常用的關(guān)鍵字,可以修飾在類上、或者修飾在變量、方法上,以此看來(lái)定義它的一些不可變性!像我們經(jīng)常使用的String類中,它便是final來(lái)修飾的類,并且它的字符數(shù)組也是被final所修飾的。但是一些final的一些細(xì)節(jié)你真的了解過(guò)嗎
    2022-05-05
  • 修改IDEA代碼左側(cè)折疊線顏色的操作

    修改IDEA代碼左側(cè)折疊線顏色的操作

    這篇文章主要介紹了修改IDEA代碼左側(cè)折疊線顏色的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • SpringMVC使用注解配置方式

    SpringMVC使用注解配置方式

    這篇文章主要為大家介紹了SpringMVC使用注解配置方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:選擇排序 Selection Sort

    Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:選擇排序 Selection Sort

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:選擇排序 Selection Sort,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下
    2015-06-06
  • springboot下mybatis-plus如何打印sql日志和參數(shù)到日志文件

    springboot下mybatis-plus如何打印sql日志和參數(shù)到日志文件

    本文主要介紹了springboot下mybatis-plus如何打印sql日志和參數(shù)到日志文件,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論