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

解決springmvc關(guān)于前臺日期作為實(shí)體類對象參數(shù)類型轉(zhuǎn)換錯(cuò)誤的問題

 更新時(shí)間:2017年06月25日 11:20:47   投稿:jingxian  
下面小編就為大家?guī)硪黄鉀Qspringmvc關(guān)于前臺日期作為實(shí)體類對象參數(shù)類型轉(zhuǎn)換錯(cuò)誤的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

頁面報(bào)錯(cuò):

后臺錯(cuò)誤:

Field error in object 'user' on field 'birthday': rejected value [2013-06-24]; codes [typeMismatch.user.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.birthday,birthday]; arguments []; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2013-06-24'; nested exception is java.lang.IllegalArgumentException]

解決方案1:在對應(yīng)的實(shí)體類屬性上加入 @DateTimeFormat(pattern = "yyyy-MM-dd")

解決方案2:不使用 <mvc:annotation-driven/>注解

使用 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 注解驅(qū)動配置

在對應(yīng)的實(shí)體類屬性上加入 @DateTimeFormat(pattern = "yyyy-MM-dd")

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
          <property name="conversionService">
            <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
          </property>
        </bean>
      </property>
    </bean>

3、使用 @InitBinder注解,注冊一個(gè)父類Controller(BaseController),其他Controller去繼承它

Springmvc配置文件 

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
public class BaseController {
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    /**
     * 第一種方式:使用WebDataBinder注冊一個(gè)自定義的編輯器,編輯器是日期類型
     * 使用自定義的日期編輯器,日期格式:yyyy-MM-dd,第二個(gè)參數(shù)為是否為空  true代表可以為空
     */
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
        new SimpleDateFormat("yyyy-MM-dd"), true));
  }
}

或者使用下面的方式

public class BaseController {
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    /**
     * 方式二:使用WebDataBinder注冊一個(gè)自定義的編輯器,編輯器是日期類型
     * 使用屬性編輯器實(shí)現(xiàn):重載setAsText,getAsText
     */
    binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {

      @Override
      public String getAsText() {
        return new SimpleDateFormat("yyyy-MM-dd")
            .format((Date) getValue());
      }

      @Override
      public void setAsText(String text) {
        try {
          setValue(new SimpleDateFormat("yyyy-MM-dd").parse(text));
        } catch (Exception e) {
          e.printStackTrace();
          setValue(null);
        }
      }

    });
  }
}

以上這篇解決springmvc關(guān)于前臺日期作為實(shí)體類對象參數(shù)類型轉(zhuǎn)換錯(cuò)誤的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java插件擴(kuò)展機(jī)制之SPI案例講解

    Java插件擴(kuò)展機(jī)制之SPI案例講解

    這篇文章主要介紹了Java插件擴(kuò)展機(jī)制之SPI案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解

    springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解

    這篇文章主要介紹了springboot中rabbitmq實(shí)現(xiàn)消息可靠性機(jī)制詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • Intellij IDEA 關(guān)閉和開啟自動更新的提示?

    Intellij IDEA 關(guān)閉和開啟自動更新的提示?

    這篇文章主要介紹了Intellij IDEA 關(guān)閉和開啟自動更新的提示操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • SpringSecurity+Redis認(rèn)證過程小結(jié)

    SpringSecurity+Redis認(rèn)證過程小結(jié)

    這篇文章主要介紹了SpringSecurity+Redis認(rèn)證過程小結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Spring Boot+Nginx實(shí)現(xiàn)大文件下載功能

    Spring Boot+Nginx實(shí)現(xiàn)大文件下載功能

    相信很多小伙伴,在日常開放中都會遇到大文件下載的情況,大文件下載方式也有很多,比如非常流行的分片下載、斷點(diǎn)下載;當(dāng)然也可以結(jié)合Nginx來實(shí)現(xiàn)大文件下載,在中小項(xiàng)目非常適合使用,這篇文章主要介紹了Spring Boot結(jié)合Nginx實(shí)現(xiàn)大文件下載,需要的朋友可以參考下
    2024-05-05
  • Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備

    Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備

    這篇文章主要為大家介紹了Spring?AOP操作的相關(guān)術(shù)語及環(huán)境準(zhǔn)備學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Spring Boot集成Druid查看配置是否生效的方法

    Spring Boot集成Druid查看配置是否生效的方法

    本文主要介紹了Spring Boot集成Druid查看配置是否生效的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java中l(wèi)ambda表達(dá)式的基本運(yùn)用

    Java中l(wèi)ambda表達(dá)式的基本運(yùn)用

    大家好,本篇文章主要講的是Java中l(wèi)ambda表達(dá)式的基本運(yùn)用,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • IDEA2020.1常用配置說明

    IDEA2020.1常用配置說明

    這篇文章主要介紹了IDEA2020.1常用配置說明,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer

    SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer

    這篇文章主要為大家介紹了SpringBoot?容器刷新前回調(diào)ApplicationContextInitializer使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評論