zoukankan      html  css  js  c++  java
  • 类型转换器

    一、全局异常处理器

    1.先创建一个包 创建一个类  然后继承Converter<String,Date>

    @Component
    public class DateTimeConverter implements  Converter<String,Date> {
    
        @Override
        public Date convert(String source) {
            try {
                return new SimpleDateFormat("yyyy-MM-dd").parse(source);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    然后再springmvc的配置文件里面添加几句配置

    <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService2">
        <property name="converters">
            <set>
                <bean class="com.ujy.converter.DateTimeConverter"/>
            </set>
        </property>
    </bean>

            配置类型转换器 然后在mvc:annotation-driven 中配置conversion-service
            点进去源码 converters 是set类型 然后复杂属性赋值

    二、局部异常处理器

    这种需要单独的 一个个的设置 ,只需要在bean里面的属性上面设置就行

    public class User {
        private  String username;
        private  String password;
        private  boolean gender;
        private  Integer age;
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date birth;
        @NumberFormat(pattern = "#,####,####.##")   //#特指数字
        private  double salary;
  • 相关阅读:
    WinForm里保存TreeView状态
    动态规划 回溯和较难题
    go 基本链表操作
    leetcode 42接雨水
    leetcode 旋转数组搜索
    leetcode 牛客编程 子序列 树 数组(积累)
    剑指offer(积累)
    go快排计算最小k个数和第k大的数
    leetcode 打家劫舍
    leetcode 字符串相关问题
  • 原文地址:https://www.cnblogs.com/ych961107/p/11888797.html
Copyright © 2011-2022 走看看