自定义日期类型转换器


前言

java学习小计,自定义类型转换器,是在ssm项目中遇到的问题的解决方案

第一步:控制器添加如下测试方法

@Controller
@RequestMapping("/company")  
public class CompanyController {

    @RequestMapping("/save")
    public String save(Date data){
        System.out.println("转换的日期:"+ data);
        return "success";
    }
}

第二步:访问测试

string类型的日期无法自动转换成日期类型
这里我们发现,string类型的日期无法自动转换成日期类型

第三步:编写一个转换器类

日期类型转换器的定义步骤:
1. 自定义一个类实现Converter接口
2. 实现Converter接口
3. 创建日期类型转换器的对象
4. 把日期类型转换器的对象交给转换器的工厂
5. 类型转换器的工厂交给注解驱动去启动


import org.springframework.core.convert.converter.Converter;

@Component
public class StringToDateConverter implements Converter<String,Date> {
    @Override
    public Date convert(String source) { //source=1999-12-12字符串
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(source);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

第四步:在springmvc.xml中配置转换器工厂

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--1. 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
       
        <property name="suffix" value=".jsp"/>
    </bean>


    <!--2.包扫描-->
    <context:component-scan base-package="cn.itcast.web"/>


    <!--3. 开启注解驱动-->
    <!--<mvc:annotation-driven/>-->


    <!--==================================注册日期类型转换器=============================================-->

    <!--4. 把日期类型转换器的对象交给转换器的工厂-->
    <bean id="conversionServiceFactory" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="stringToDateConverter"/>
            </set>
        </property>
    </bean>

    <!--5. 类型转换器的工厂交给注解驱动去启动,记得删除上面的注解驱动,一句即可-->
    <mvc:annotation-driven conversion-service="conversionServiceFactory"/>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GotBy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值