解決springmvc關(guān)于前臺(tái)日期作為實(shí)體類對(duì)象參數(shù)類型轉(zhuǎn)換錯(cuò)誤的問題
頁面報(bào)錯(cuò):

后臺(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:不使用 <mvc:annotation-driven/>注解
使用 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 注解驅(qū)動(dòng)配置
在對(duì)應(yīng)的實(shí)體類屬性上加入 @DateTimeFormat(pattern = "yyyy-MM-dd")
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService">
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
</property>
</bean>
</property>
</bean>
3、使用 @InitBinder注解,注冊(cè)一個(gè)父類Controller(BaseController),其他Controller去繼承它
Springmvc配置文件
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
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ò)誤的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
手動(dòng)實(shí)現(xiàn)將本地jar添加到Maven倉庫
這篇文章主要介紹了手動(dòng)實(shí)現(xiàn)將本地jar添加到Maven倉庫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
淺談Java循環(huán)中的For和For-each哪個(gè)更快
本文主要介紹了淺談Java循環(huán)中的For和For-each哪個(gè)更快,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Spring MVC完全注解方式配置web項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Spring MVC完全注解方式配置web項(xiàng)目的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
SpringCloud URL重定向及轉(zhuǎn)發(fā)代碼實(shí)例
這篇文章主要介紹了SpringCloud URL重定向及轉(zhuǎn)發(fā)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
java Hibernate多對(duì)多映射詳解及實(shí)例代碼
這篇文章主要介紹了java Hibernate多對(duì)多映射詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Java獲取e.printStackTrace()打印的信息方式
這篇文章主要介紹了Java獲取e.printStackTrace()打印的信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Mybatis分頁的實(shí)現(xiàn)及使用注解開發(fā)操作
這篇文章主要介紹了Mybatis分頁的實(shí)現(xiàn)及使用注解開發(fā)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

