@InitBinder 前端传递date时间类型属性时,转换错误问题

在Controller里加上这段代码

@InitBinder 
public void initBinder(WebDataBinder binder) { 
  binder.registerCustomEditor(Date.class, new PropertyEditorSupport() { 
    public void setAsText(String value) { 
      try {
        SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy-MM-dd');
        setValue(dateFormat.parse(value)); 
      } catch(ParseException e) { 
        setValue(null); 
      } 
    } 

    public String getAsText() { 
      return new SimpleDateFormat('yyyy-MM-dd').format((Date) getValue()); 
    } 
  }); 
}

 

posted @ 2018-03-13 15:50  秋水秋色  阅读(236)  评论(0编辑  收藏  举报