SpringMVC用POST方式提交資料(包括含時間)時遇到The request sent by the client was syntactically incorrect.
阿新 • • 發佈:2018-12-18
對於一個剛入手SSM的小白來說學習可謂是步履維艱,這不在修改表單資料並提交時就遇到了The request sent by the client was syntactically incorrect.問題。在網上看了好多回答,有說jar包沒對的,也有說是表單資料格式不對的(要指定為enctype="multipart/form-data")。這些方法都試了,但問題還是沒得到解決。最後發現我的問題原來是在表單裡提交時間的格式是字串String,但是後臺處理的是Date格式,所以就出現了引數繫結錯誤,要先將String轉換為Date。解決方案如下,在相應的Controller中加上下面這個方法(必須有註解):
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
//日期格式轉換
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
// CustomDateEditor為自定義日期編輯器
}
方法所在類為:
@Controller @RequestMapping("/items") public class ItemsController { @RequestMapping("/saveOrUpdate") public String saveOrUpdate(Items items) { itemsService.saveOrUpdate(items); return "redirect:list.do"; } }
SSM整合第一天,加油!!!