ITeye博客

Spring表单提交日期类型绑定Failed to convert from type java.lang.String to type java.util.D

sxlkk · 2015-01-23 16:57 · spring · 编程语言 · Spring · Markdown

org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date

 

解决Spring自动绑定中的数据转换问题,直接在Controller中添加下面一段代码即可,其他数据类型同理。

 

    /*
     * 表单提交日期绑定
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /*
     * 提交绑定
     */
    @InitBinder
    public void initDataBinder(WebDataBinder binder) {
        binder.registerCustomEditor(BookBagType.class, bookBagTypeEditor);
    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。