有的业务可能会用到自定义类型转换器 比如表单提交中提交的日期 默认是String类型   需要封装到 日期对象中      这时  就需要自定义转换器


方法


首先在controller中启用@InitBinder  注解 来初始化 数据绑定器   


然后写自己的方法(以日期转换为例) 当提交时   如果不是默认的普通类型的转换  就会调用下面的类型转换器就行格式转换

 @InitBinder
    protected void dataConverter(HttpServletRequest req, ServletRequestDataBinder binder) throws Exception {
      binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }


相关文章:

  • 2022-12-23
  • 2021-07-29
  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-01-03
猜你喜欢
  • 2021-10-24
  • 2021-04-30
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案