十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
頁(yè)面報(bào)錯(cuò):
成都創(chuàng)新互聯(lián)專注于扎蘭屯企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。扎蘭屯網(wǎng)站建設(shè)公司,為扎蘭屯等地區(qū)提供建站服務(wù)。全流程按需開(kāi)發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

后臺(tái)錯(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:在對(duì)應(yīng)的實(shí)體類屬性上加入 @DateTimeFormat(pattern = "yyyy-MM-dd")

解決方案2:不使用
使用 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 注解驅(qū)動(dòng)配置
在對(duì)應(yīng)的實(shí)體類屬性上加入 @DateTimeFormat(pattern = "yyyy-MM-dd")
3、使用 @InitBinder注解,注冊(cè)一個(gè)父類Controller(BaseController),其他Controller去繼承它
Springmvc配置文件
public class BaseController {
@InitBinder
public void initBinder(WebDataBinder binder) {
/**
* 第一種方式:使用WebDataBinder注冊(cè)一個(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注冊(cè)一個(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)于前臺(tái)日期作為實(shí)體類對(duì)象參數(shù)類型轉(zhuǎn)換錯(cuò)誤的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。