014 注解@InitBinder

一 . 概述

  在前面我们使用了类型转换服务完成了类型的转换,另外springmvc也提供了一种注解的方式来完成

    这种类型转换服务.


二 . 使用类型转换注解 @InteBinder  

@InitBinder
    public void dateBinder(WebDataBinder binder) {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(sim, true));
    }

在我们的Controller之中,添加了这个类型转换器,就可以自动的完成从string到日期的转换了.


三总结

  虽然有这种简单的方式完成数据类型的转换,但是还是建议使用配置的方式完成.

猜你喜欢

转载自www.cnblogs.com/trekxu/p/9129693.html
014