spring mvc的@initBinder 注解的作用
在SpringMVC中,java bean中的Date,double等类型的属性,如果没有做任何处理的话,日期以及double都无法与请求方法的入参绑定。解决的办法就是用spring mvc提供的@InitBinder标签。
例如:
@Controller
public class MyFormController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
@ResponseBody
@RequestMapping("/date")
public void printDate(Date d){
System.out.println(d);
return;
}
}
还要在springMVC配置文件中加上:
这样就可以直接将上传的日期时间字符串绑定为日期类型的数据了