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

SpringMVC接收java.util.Date類型數(shù)據(jù)的2種方式小結(jié)

 更新時(shí)間:2021年08月10日 17:11:43   作者:suanday_sunny  
這篇文章主要介紹了使用SpringMVC接收java.util.Date類型數(shù)據(jù)的2種方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringMVC接收java.util.Date類型數(shù)據(jù)

在Controller中如下定義方法

public PassQueryRequest trade(@ModelAttribute PassQueryRequest tradeRequest,
   @RequestParam(value="startDate", required=true)Date startDate,
   @RequestParam(value="endDate", required=true)Date endDate

1、在springmvc中使用對象接收參數(shù)時(shí)

在PassQueryRequest中,在日期屬性的set方法中增加定義,以及maven配置

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    public Date getStartDate() {
        return startDate;
    }
<dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda-time.version}</version>
        </dependency>

2、直接使用java.util.Date變量接收參數(shù)

@org.springframework.web.bind.annotation.InitBinder
 public void InitBinder(
   /* HttpServletRequest request, */ServletRequestDataBinder binder) {
  // 不要?jiǎng)h除下行注釋!!! 將來"yyyy-MM-dd"將配置到properties文件中
  // SimpleDateFormat dateFormat = new
  // SimpleDateFormat(getText("date.format", request.getLocale()));
  System.out.println("執(zhí)行了InitBinder方法");
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  dateFormat.setLenient(false);
  binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
 }

解決 springmvc中接收date數(shù)據(jù)問題

springmvc Controller類中需要接收的是Date類型,但是在頁面端傳過來的是String類型,就會出現(xiàn)以下異常

Failed to convert value of type 'java.lang.String' to required type 'java.util.Date';

這里提供三種解決方案。

一、局部轉(zhuǎn)換

@Controller
@RequestMapping("order")
public class OrderCtrl extends CtrlSupport {
 private static final Logger logger = LoggerFactory.getLogger(OrderCtrl.class);
 
 // 將字符串轉(zhuǎn)換為Date類
  @InitBinder
  public void initBinder(WebDataBinder binder, WebRequest request) {
   // 轉(zhuǎn)換日期格式
   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 
  }
}

二、全局轉(zhuǎn)換

1.創(chuàng)建convertDate類實(shí)現(xiàn)WebBindingInitializer接口

public class convertDate implements WebBindingInitializer{ 
 @Override
 public void initBinder(WebDataBinder binder, WebRequest request) {
  // TODO Auto-generated method stub
  //轉(zhuǎn)換日期
  DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
 }
}

2.在Spring-MVC.xml中配置日期轉(zhuǎn)換

<!-- 日期轉(zhuǎn)換 -->
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="webBindingInitializer">
   <bean class="com.wx.web.convertDate"/>
  </property>
 </bean>

三、get方法配置

import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
 
@DateTimeFormat(pattern = "yyyy-MM-dd")  
@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
public Date getGetLicenseTime() {
 return getLicenseTime;
}
public void setGetLicenseTime(Date getLicenseTime) {
 this.getLicenseTime = getLicenseTime;
}

@JsonFormat 默認(rèn)是標(biāo)準(zhǔn)時(shí)區(qū)的時(shí)間, 北京時(shí)間 東八區(qū) timezone=”GMT+8”

作用:后臺的時(shí)間 格式化 發(fā)送到前臺

@DateTimeFormat 接受前臺的時(shí)間格式 傳到后臺的格式

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot中使用Swagger的超簡單方法

    SpringBoot中使用Swagger的超簡單方法

    大家一致認(rèn)為springBoot使用swagger太麻煩了,每次都需要編寫config,今天小編告訴大家一種超簡單配置方法,教大家如何整合swagger,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • 關(guān)于spring-security(記住密碼,CSRF)

    關(guān)于spring-security(記住密碼,CSRF)

    文章主要介紹了Spring Security中的PersistentTokenRepository、CSRF保護(hù)機(jī)制以及如何在登錄頁面添加記住我功能,并分享了相關(guān)實(shí)現(xiàn)代碼和配置
    2024-11-11
  • JavaSwing FlowLayout 流式布局的實(shí)現(xiàn)

    JavaSwing FlowLayout 流式布局的實(shí)現(xiàn)

    這篇文章主要介紹了JavaSwing FlowLayout 流式布局的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等)

    JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等)

    這篇文章主要介紹了JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • 一文深入理解Java中的深拷貝機(jī)制

    一文深入理解Java中的深拷貝機(jī)制

    在Java編程中,我們經(jīng)常需要處理對象的復(fù)制問題,深拷貝和淺拷貝是兩種常見的復(fù)制方式,它們在內(nèi)存管理和對象引用方面存在不同特點(diǎn),本文將帶大家深入探究Java中的深拷貝機(jī)制,需要的朋友可以參考下
    2023-09-09
  • 利用SpringMVC和Ajax實(shí)現(xiàn)文件上傳功能

    利用SpringMVC和Ajax實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了利用SpringMVC和Ajax實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • java基于C/S模式實(shí)現(xiàn)聊天程序(服務(wù)器)

    java基于C/S模式實(shí)現(xiàn)聊天程序(服務(wù)器)

    這篇文章主要為大家詳細(xì)介紹了java基于C/S模式實(shí)現(xiàn)聊天程序的服務(wù)器篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Springboot?對接支付寶實(shí)現(xiàn)掃碼支付功能

    Springboot?對接支付寶實(shí)現(xiàn)掃碼支付功能

    本文介紹了如何在Spring?Boot項(xiàng)目中實(shí)現(xiàn)支付寶支付功能,包括沙箱環(huán)境配置、依賴引入、配置參數(shù)、訂單類定義、測試接口編寫等步驟,通過不同場景下的請求方式(PC端、二維碼、回調(diào)處理、定時(shí)查詢支付結(jié)果),展示了如何與支付寶API進(jìn)行交互,感興趣的朋友一起看看吧
    2025-03-03
  • java?啟動(dòng)參數(shù)?springboot?idea詳解

    java?啟動(dòng)參數(shù)?springboot?idea詳解

    這篇文章主要介紹了java?啟動(dòng)參數(shù)?springboot?idea的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • Java項(xiàng)目開啟遠(yuǎn)程調(diào)試的方法步驟(tomcat、springboot)

    Java項(xiàng)目開啟遠(yuǎn)程調(diào)試的方法步驟(tomcat、springboot)

    這篇文章主要介紹了Java項(xiàng)目開啟遠(yuǎn)程調(diào)試的方法步驟(tomcat、springboot),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10

最新評論

石景山区| 达尔| 仪陇县| 易门县| 丹东市| 普兰店市| 梧州市| 东辽县| 抚远县| 普宁市| 山丹县| 景洪市| 广安市| 阜城县| 专栏| 聂拉木县| 小金县| 措美县| 沅江市| 青阳县| 上犹县| 樟树市| 涿州市| 正定县| 香港| 西藏| 雷波县| 杭锦旗| 城步| 巩义市| 静海县| 井陉县| 航空| 固始县| 靖江市| 中宁县| 巧家县| 云龙县| 阳城县| 桂阳县| 长葛市|