【Spring-MVC】自定义Converters

30 篇文章 1 订阅

【Spring-MVC】自定义Converters

11.1 什么是Converters

Contervers中文就是转化器的意思,在我们的SprinMVC中其实就是帮助我们转化数据的转化器,比如将String转化为int类型等等,在SpringMVC中为我们提供了大量的转换器。

  • 在SpringMVC中还提供了一种转化器:Formatter,使用它也是可以对数据进行转化的

Converters和Formatter的区别:

转化器说明
Converters可以将任意的类型转化为其他任意的类型,可以用于很多层
Formatter可以将String类型转化为其他任意的类型,适用于web层

11.2 编写日期转化器

对于时间的处理,我们可以通过局部的方法进行转化,也可以通过全局的方式进行转化,就是自己自定义一个转化器。

步骤:

  • 实现Converter<S,T>接口,其中泛型S代表我们转化数据的类型,T代表我们需要转化成的数据类型
  • 重写convert(S source)方法
// 																			实现接口,我们将String类型转化为日期类型
public class MyDateConverter implements Converter<String, Date> {
    /**
     * 转化时间控制器
     * @param source 传入String时间
     * @return Date类的时间
     */
    @Override
    public Date convert(String source) {
        // 实例格式工厂
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            // 返回解析对的对应的时间
            return format.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

11.3 XML配置转化器

上面的一个转化器写好以后我们需要将该转化器配置到我们的SpringMVC框架中,首先我们可以使用XML配置文件来进行配置

注意:

  • 我们这里需要重新定义一个转化器服务,模版SpringMVC已经提供好了,我们直接使用该类就可以FormattingConversionServiceFactoryBean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.moon.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
  	
  	<!--在mvc的注解驱动启动这里指定转化器服务为我们自定义的服务-->
    <mvc:annotation-driven conversion-service="myConvertersService"/>
       
  	<!--自定义一个转化器服务-->
    <bean id="myConvertersService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!--添加转化器属性中添加我们自定义的转化器-->
      	<property name="converters">
          	<!--通过set集合把我们的自定义转化器注入-->
            <set>
                <bean class="com.moon.converter.MyDateConverter"/>
            </set>
        </property>
    </bean>
  
		......
  
</beans>

11.4 Java类配置转化器

当然我们也可以通过Java类来配置这个转化器,这时候我们不需要去添加我们控制器的Bean,可以重写MVC配置类的方法

@Configuration
@EnableWebMvc
@ComponentScan("com.moon.controller")
public class MvcConfig implements WebMvcConfigurer {
   
    ......
     
    /**
     * 添加自定义的Converter
     *
     * @param registry 注册信息
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 添加自己写的Converter
        registry.addConverter(new MyDateConverter());
    }
  
  	......
      
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值