欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC中日期格式的轉(zhuǎn)換

 更新時(shí)間:2017年03月15日 11:51:12   作者:rodge  
本文主要介紹了SpringMVC中日期格式轉(zhuǎn)換的相關(guān)知識(shí):用來(lái)解決日期提交轉(zhuǎn)換異常的問(wèn)題。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

解決日期提交轉(zhuǎn)換異常的問(wèn)題

由于日期數(shù)據(jù)有很多種格式,所以springmvc沒(méi)辦法把字符串轉(zhuǎn)換成日期類型。所以需要自定義參數(shù)綁定。前端控制器接收到請(qǐng)求后,找到注解形式的處理器適配器,對(duì)RequestMapping標(biāo)記的方法進(jìn)行適配,并對(duì)方法中的形參進(jìn)行參數(shù)綁定。在springmvc這可以在處理器適配器上自定義Converter進(jìn)行參數(shù)綁定。如果使用<mvc:annotation-driven/>可以在此標(biāo)簽上進(jìn)行擴(kuò)展。

1.自定義DataConvertor類, 并實(shí)現(xiàn)Convertor接口

public class DateConverter implements Converter<String, Date> {
   @Override
   public Date convert(String source) {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
        return simpleDateFormat.parse(source);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      return null;
   }
}

2.在springmvc.xml配置文件中注冊(cè)轉(zhuǎn)換器

方法一:通過(guò)注解驅(qū)動(dòng)的方式加載轉(zhuǎn)換器

<!-- 配置mvc注解驅(qū)動(dòng) -->
  <mvc:annotation-driven conversion-service="conversionService"/>
  <!-- 配置日期轉(zhuǎn)換器 -->
  <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="cn.rodge.ssm.converter.DateConverter"></bean>
      </set>
    </property>
  </bean>

方法二:通過(guò)自定義webBinder配置(不常用)

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <!-- 掃描帶Controller注解的類 -->
   <context:component-scan base-package="cn.itcast.springmvc.controller" />
   <!-- 轉(zhuǎn)換器配置 -->
   <bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
      <property name="converters">
        <set>
           <bean class="cn.itcast.springmvc.convert.DateConverter"/>
        </set>
      </property>
   </bean>
   <!-- 自定義webBinder -->
   <bean id="customBinder"   class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
      <property name="conversionService" ref="conversionService" />
   </bean>
   <!--注解適配器 -->
   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
      <property name="webBindingInitializer" ref="customBinder"></property>
   </bean>
   <!-- 注解處理器映射器 -->
   <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
   <!-- 加載注解驅(qū)動(dòng) -->
   <!-- <mvc:annotation-driven/> -->
   <!-- 視圖解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <!-- jsp前綴 -->
      <property name="prefix" value="/WEB-INF/jsp/" />
      <!-- jsp后綴 -->
      <property name="suffix" value=".jsp" />
   </bean>
</beans>

注意:此方法需要獨(dú)立配置處理器映射器、適配器,不再使用<mvc:annotation-driven/>

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • java實(shí)現(xiàn)人員信息管理系統(tǒng)

    java實(shí)現(xiàn)人員信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)人員信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java  mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼

    Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼

    這篇文章主要介紹了Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • 詳解Java使用Pipeline對(duì)Redis批量讀寫(hmset&hgetall)

    詳解Java使用Pipeline對(duì)Redis批量讀寫(hmset&hgetall)

    本篇文章主要介紹了Java使用Pipeline對(duì)Redis批量讀寫(hmset&hgetall),具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • 詳解json在SpringBoot中的格式轉(zhuǎn)換

    詳解json在SpringBoot中的格式轉(zhuǎn)換

    這篇文章主要介紹了詳解json在SpringBoot中的格式轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題

    關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題

    這篇文章主要介紹了關(guān)于SpringBoot配置文件application.properties的路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • springboot?整合?clickhouse的實(shí)現(xiàn)示例

    springboot?整合?clickhouse的實(shí)現(xiàn)示例

    本文主要介紹了springboot?整合?clickhouse的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • SpringBoot核心@SpringBootApplication使用介紹

    SpringBoot核心@SpringBootApplication使用介紹

    這篇文章主要介紹了SpringBoot核心@SpringBootApplication的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring中的Devtools源碼解析

    Spring中的Devtools源碼解析

    這篇文章主要介紹了Spring中的Devtools源碼解析,Spring中的Devtools是一個(gè)開發(fā)工具,旨在提高開發(fā)人員的生產(chǎn)力和開發(fā)體驗(yàn),它提供了一系列功能,包括自動(dòng)重啟、熱部署、遠(yuǎn)程調(diào)試等,使開發(fā)人員能夠更快速地進(jìn)行代碼修改和調(diào)試,需要的朋友可以參考下
    2023-10-10
  • java編程常用技術(shù)(推薦)

    java編程常用技術(shù)(推薦)

    下面小編就為大家?guī)?lái)一篇java編程常用技術(shù)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • java實(shí)現(xiàn)線性表及其算法

    java實(shí)現(xiàn)線性表及其算法

    線性表是最簡(jiǎn)單和最常用的一種數(shù)據(jù)結(jié)構(gòu),它是有n個(gè)體數(shù)據(jù)元素(節(jié)點(diǎn))組成的有限序列,這篇文章主要介紹了java實(shí)現(xiàn)線性表及其算法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06

最新評(píng)論