查閱了好多資料, 最后才發現原來只是添加一個方法就能解決的問題, 但是看了半天又沒看明白, 只是知道這么寫就能成功, 先記下來, 以后再研究吧, 在配置好springMVC的時候, 可以在前台的form表單傳遞一個實體類, 后台spring會自動將name傳遞來的屬性自動裝配給實體類的class, 但是有一點, 如果實體類中有一個屬性是一個date型的, 就會報出400的錯誤, "客戶端傳遞的參數中有語法錯誤", 最后在controller的那個控制類里面機上這樣一段代碼, 但不知道是什么意思
@org.springframework.web.bind.annotation.InitBinder public void InitBinder(/*HttpServletRequest request, */ServletRequestDataBinder binder) { // 不要刪除下行注釋!!! 將來"yyyy-MM-dd"將配置到properties文件中 // SimpleDateFormat dateFormat = new // SimpleDateFormat(getText("date.format", request.getLocale())); System.out.println("執行了InitBinder方法"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true)); }
使用@InitBinder標簽, spring在將參數傳遞到后台的controller的時候就可以自動轉換成日起類型的格式了