springmvc后台用date类型接受前台参数时,一直报错

springmvc后台用date类型接受前台参数时,一直报错:Failed to load resource: the server responded with a status of 400 (Bad Request)

 

spring mvc 后台只接收String类型,所以需要对日期类型处理

一、只需要在方法中加入

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. @InitBinder  
  2. protected  void initBinder(WebDataBinder binder) {  
  3.     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  4.     binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  
  5. }  

方法就可以

二、注解方式:   

 

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")  //取日期时使用  
  2. @DateTimeFormat(pattern = "yyyy-MM-dd")//存日期时使用  
  3. private Date startTime;  

 转发自:http://blog.csdn.net/jiana227/article/details/18663159

posted @ 2016-06-23 14:37  年轻就是财富!  阅读(936)  评论(0)    收藏  举报