博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC处理表单日期数据转换异常(Date)使用@InitBinder
阅读量:4137 次
发布时间:2019-05-25

本文共 3386 字,大约阅读时间需要 11 分钟。

1:@ControllerAdvice注解添加一个类:

package cn.temptation.web;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.InitBinder;import java.text.SimpleDateFormat;import java.util.Date;/** * @Author:Starry * @Description:处理表单数据转换对象异常(Date) * @Date:Created in 14:45 2018/3/15 * Modified By: */@ControllerAdvicepublic class AppControllerAdvice {    @InitBinder    public void initBinder(WebDataBinder binder){        /*以下方法二选一,第一个无需添加MulitFormatDateEditor这个类*/        //方法1:支持一个日期格式        binder.registerCustomEditor(Date.class,                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));        //方法2:支持两个日期格式        //true 允许属性为空verficationController        binder.registerCustomEditor(Date.class,                new MulitFormatDateEditor(new SimpleDateFormat("yyyy-MM-dd"),                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),true));    }

}

2:创建一个继承自PropertyEditorSupport的类MulitFormatDateEditor

package cn.temptation.web;import org.springframework.util.StringUtils;import java.beans.PropertyEditorSupport;import java.text.DateFormat;import java.text.ParseException;import java.util.Date;/** * @Author:Starry * @Description:支持两个日期格式的转换,springmvc自带的转换器仅支持一种格式 * @Description:日期转换为String时默认使用dateFormat2格式 * @Date:Created in 14:49 2018/3/15 * Modified By: */public class MulitFormatDateEditor extends PropertyEditorSupport{    //日期格式1    private final DateFormat dateFormat;    //日期格式2    private final DateFormat dateFormat2;    //是否允许为空    private final boolean allowEmpty;    //日期长度    private final int exactDateLength;    public MulitFormatDateEditor(DateFormat dateFormat, DateFormat dateFormat2, boolean allowEmpty) {        this.dateFormat = dateFormat;        this.dateFormat2 = dateFormat2;        this.allowEmpty = allowEmpty;        this.exactDateLength = -1;    }    public MulitFormatDateEditor(DateFormat dateFormat, DateFormat dateFormat2, boolean allowEmpty, int exactDateLength) {        this.dateFormat = dateFormat;        this.dateFormat2 = dateFormat2;        this.allowEmpty = allowEmpty;        this.exactDateLength = exactDateLength;    }    /*    * 使用指定的日期格式,解析给定文本的日期。    * */    @Override    public void setAsText(String text) throws IllegalArgumentException {        if(this.allowEmpty && StringUtils.hasText(text)){            setValue(null);        }        else if(text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength){            throw new IllegalArgumentException("不能解析这个日期,长度为:"+this.exactDateLength);        }        else{            try {                if(text != null){                    setValue(this.dateFormat.parse(text));                }            }catch (ParseException ex){                try {                    if(text != null){                        setValue(this.dateFormat2.parse(text));                    }                }catch (ParseException ex2){                    throw new IllegalArgumentException("不能解析这个日期"+ex.getMessage(),ex);                }            }        }    }    /**     * 将日期格式化为字符串,使用指定的日期格式。     */    @Override    public String getAsText() {        Date value = (Date)getValue();        return (value != null ? this.dateFormat2.format(value):"");    }}
 

3:然后就可以了,每天进步一点点 Starry❤

转载地址:http://zvxvi.baihongyu.com/

你可能感兴趣的文章
laravel 修改api返回默认的异常处理
查看>>
laravel事务
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
还不会正则表达式?看这篇!
查看>>
100道+ JavaScript 面试题,助你查漏补缺
查看>>
JavaScript深入理解之闭包
查看>>
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>
25个构建Web项目的HTML建议,你需要了解一下!
查看>>
【web素材】02-10款大气的购物商城网站模板
查看>>
6种方式实现JavaScript数组扁平化(flat)方法的总结
查看>>
如何实现a===1 && a===2 && a===3返回true?
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>