SpringMvc 数据绑定400错误
今天请求一个SpringMvc 的时候,客户端总是报出: The request sent by the client was syntactically incorrect 网上都是说的是bean的名字和表单的名字不一样,但是我检查了N多遍之后,还是有报这个异常,就想到了SpringMvc 自动装配的问题。 然后看日志说是""转为int类型的时候不可以为空; 应该就是数据绑定的问题了,经过一番研究,springMvc的数据绑定有几种方式: 1,controller 独享 2,全局共享 controller独享方式: @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, false)); } 全局共享: 全局共享方式有几种实现方法: ① 继承 WebBindingInitializer 接口来实现全局注册 使用@InitBinder只能对特定的controller类生效