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

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à)值,希望對(duì)大家有所幫助。如有錯(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中使用對(duì)象接收參數(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類型,就會(huì)出現(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”

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

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

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

相關(guān)文章

最新評(píng)論