Spring MVC "The request sent by the client was syntactically incorrect ()"解決辦法

使用Spring MVC3框架時,可能會遇到這個問題,請求後臺沒有任何響應,頁面顯示


讓人很莫名其妙。

The request sent by the client was syntactically incorrect 說的意思是:由客戶端發送的請求是語法上是不正確的。但還是不明白哪裏出了問題,

我的URLhttp://localhost:8080/mmsys/field/list.do?date=2015-06-07,真看不出那個參數出錯了,實在沒辦法我把spring日誌級別調整到debug級別,終於找出原因了:

[DEBUG][2013/04/28 12:19:10,561][org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - Resolving exception from handler [com.xdtech.cloudsearch.module.config.index.action.FieldAction@44b46c4c]: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "23-05-2013" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException

Spring在轉換日期出錯了,翻看了一下spring-framework-reference.pdf找到下面這幾行代碼,加到自己的Action裏,問題解決了:

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}





發佈了16 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章