SpringMVC接收java.util.Date類型數(shù)據(jù)的2種方式小結(jié)
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)文章
java validation 后臺(tái)參數(shù)驗(yàn)證的使用詳解
本篇文章主要介紹了java validation 后臺(tái)參數(shù)驗(yàn)證的使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
java 實(shí)現(xiàn)反射 json動(dòng)態(tài)轉(zhuǎn)實(shí)體類--fastjson
這篇文章主要介紹了java 實(shí)現(xiàn)反射 json動(dòng)態(tài)轉(zhuǎn)實(shí)體類--fastjson,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
java使用jacob實(shí)現(xiàn)word轉(zhuǎn)pdf
這篇文章主要為大家詳細(xì)介紹了java使用jacob實(shí)現(xiàn)word轉(zhuǎn)pdf,通過調(diào)用模板文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
IntelliJ IDEA 關(guān)閉多余項(xiàng)目的操作方法
這篇文章主要介紹了IntelliJ IDEA 關(guān)閉多余項(xiàng)目的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Spring Boot高級(jí)教程之Spring Boot連接MySql數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了Spring Boot高級(jí)教程之Spring Boot連接MySql數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

