在我的javascript代碼,我有格式MM/DD/YYYY
的日期。我需要通過請求URL傳遞給Spring控制器。 我試圖通過字符串,但因爲/
不能這樣做。 發送date(MM/DD/YYYY)
到Spring控制器的最佳方法是什麼?無法從JS獲取日期到控制器
js代碼:
var today = moment(); //it gives date as 05/26/2017
var requestURL = myContextPath + '/processDate/'+today+'/processDateForStack.form
控制器:
@RequestMapping(value = "/{today}/processDateForStack", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public
@ResponseBody
String getprocessStartedDate(@PathVariable("today") String today) throws Exception{
System.out.println("date : " + today); //should display 05/26/2017
//logic here
}
PS:在這裏,我已經使用日期作爲字符串型,i可以爲日期類型也使用。
我試着從js發送日期爲MM-DD-YYYY
來自js代碼,並且在彈簧控制器中我使用了@DateTimeFormat(pattern = "MM-dd-yyyy")
,同時聲明瞭參數但拋出了異常。任何的意見都將會有幫助。
--EDITED--
當我從JavaScript傳遞日期值作爲字符串,在彈簧控制器今天的值是1495823051245
。 當我通過日期值作爲日期從JavaScript到彈簧控制器,下面是生成的異常。
Error: Failed to convert value of type 'java.lang.String' to required type 'java.sql.Date'; nested exception is org.springframework.core.convert.ConversionFailedException
當發送的日期的日期從JS到控制器I已經使用@DateTimeFormat(pattern = "MM-dd-yyyy")
而宣告pathvariable。
你可以包括任何你得到的錯誤和'System.out.println(...);' – BRogers
@BRogers - 錯誤的結果:錯誤:無法將類型'java.lang.String'的值轉換爲必需類型'java.sql.Date';嵌套的例外是org.springframework.core.convert.ConversionFailedException.This例外,當我用@DateTimeFormat(模式=「MM-DD-YYYY」)在控制器 – user3684675
您是否嘗試過明確其強制轉換爲'java.sql.Date我有'一旦你收到它? – BRogers