springMVC的自定义类型转换

在页面与前端控制器之间,出现的类型转换问题,如果想要自己实现一个类型转换:

public class StringDateConvert implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        //1.判断是否为空
        if (source == null) {
            throw new RuntimeException("请输入正常日期");
        }
        System.out.println("首先,写实现converter类的类,其次,将其加入springMvVC中,最后,通过MvC启动该类的使用");
            //2.自定义
            DateFormat df = new SimpleDateFormat("yyyy-MM-DD");
            try {
                return df.parse(source);
            } catch (ParseException e) {
                throw new RuntimeException("w,跳转错误");
            }
    }
}

[2.配置注册在这里插入图片描述配置自定义转换器]3.加入容器并启用
在这里插入图片描述

发布了12 篇原创文章 · 获赞 0 · 访问量 148

猜你喜欢

转载自blog.csdn.net/weixin_44065691/article/details/105048996
今日推荐