类型转换器

一、全局异常处理器

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;

 

 

 

 

 

 

posted @ 2019-11-19 13:42  呆code  阅读(208)  评论(0编辑  收藏  举报