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

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

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

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ù)時

在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) {
  // 不要刪除下行注釋!!! 將來"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類實現(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)時區(qū)的時間, 北京時間 東八區(qū) timezone=”GMT+8”

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

@DateTimeFormat 接受前臺的時間格式 傳到后臺的格式

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

相關(guān)文章

最新評論