控制檯:
WARN efaultHandlerExceptionResolver - Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type ‘java.lang.String’ to required type ‘java.util.Date’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value ‘2020-10-01’; nested exception is java.lang.IllegalArgumentException]
頁面(400錯誤):
錯誤程式碼(springmvc.xml)
<!-- 把轉換器工廠放入到註解驅動,轉換器才會生效 -->
<mvc:annotation-driven conversion-service="conversionServiceFactory" />
<!-- 開啟註解驅動,開啟SpringMVC的註解的支援 @RequestMapping @RequestBody @ResponseBody這些註解需要使用 -->
<mvc:annotation-driven/>
錯誤原因
轉換器工廠加入到註解驅動<mvc:annotation-driven conversion-service="conversionServiceFactory" />
寫在了開啟註解驅動<mvc:annotation-driven/>
的前面,註解驅動都沒有開啟,怎麼將轉換器工廠放入到註解驅動呢?
解決辦法:
將開啟註解驅動程式碼提前<mvc:annotation-driven/>
,放在轉換器工廠的前面,就可以解決問題了。
springmvc模板:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--springmvc是web層(Controller處理請求) UserController @Controller -->
<!--1.掃描Controller所在包-->
<context:component-scan base-package="com.xgf.web"/>
<!--2. 設定的檢視解析器物件,success 路徑/WEB-INF/pages/success.jsp -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--3. 組態檔 - 過濾靜態資源 前面的前端控制器攔截所有,靜態css、js這些也都攔截
設定使.js .css img這些靜態檔案不被攔截,保證靜態檔案都不攔截
用過mvc標籤進行放行,這些目錄下檔案都不攔截 -->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/fonts/" mapping="/fonts/**" />
<!-- 4. 開啟註解驅動,開啟SpringMVC的註解的支援 @RequestMapping @RequestBody @ResponseBody這些註解需要使用 -->
<mvc:annotation-driven/>
<!-- 5. 設定型別轉換器 -->
<!-- 建立型別轉換器物件 -->
<bean id="stringToDateConverter" class="com.xgf.web.converter.StringToDateConverter"/>
<!-- 把轉換器物件放入SpringMVC轉換器工廠中 -->
<bean id="conversionServiceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 引入日期轉換器物件(上面建立的) -->
<ref bean="stringToDateConverter"/>
<!-- 或者直接將轉換器類寫到這裡面 -->
<!--<bean id="stringToDateConverter" class="com.xgf.web.converter.StringToDateConverter"/>-->
</set>
</property>
</bean>
<!--6. 把轉換器工廠放入到註解驅動,轉換器才會生效 -->
<mvc:annotation-driven conversion-service="conversionServiceFactory" />
</beans>