在Web項目中提交form提交報400錯誤一般造成的情況是提交參數與接收參數不匹配,但是在spring mvc中出現這種error還可能是參數未作綁定。
具體的解決辦法就是在controller中加入如下代碼:
/**
* 數據綁定
*
* @param binder
* WebDataBinder
*/
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
binder.registerCustomEditor(Date.class, new DateEditor(true));
}
一般情況下這個數據會寫在BaseController中,然後所有controller都繼承BaseController