spring mvc环境之UI到控制器的自定义类型转换(十三)

 

spring其实有默认的类型转换,比如前端表单提交数字的字符串,在控制器可接收为int或string都是没有错的.另外控制器也可以把前端数据接收为一个对象.

即使spring为我们考虑的很周全了,但是spring还是继承了他一贯的思想(自己永远都不是一个完美的人),总有考虑不周的地方....

扩展自定义的类型转换就是我们这篇要介绍的.

spring这种扩展一般的步骤都是,
1.继承某个接口,或他们已经把接口的实现类提供了,我们继承实现类后;
2.把自己写的实现类,以配置或注解的形式让spring容器管理.
3.在或是让spring容器管理后,再以某个bean的参数的形式注入到容器的某个模块中.

-------------

下来我们说说自定义类型转换的配置实现步骤:

1.写一个类TestConverter继承Converter可带泛型(Converter<S, T> ,S:代表源数据类型 T:代表目标数据类型);

2.在spring-mvc.xml中配置TestConverter让spring容器管理;

3.然后还要配置org.springframework.context.support.ConversionServiceFactoryBean让spring容器管理其名称,自己写的转换类作为参数注入其中;

4.ConversionServiceFactoryBean配置到mvc:annotation-driven中.

 

 

 

============================

1.演示一个时间转换器,

1).定义自己的类型转换器  继承一个父接口 Converter<S, T>

package com.cc8w.convertion;

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

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

    private SimpleDateFormat getDateFormat(String source) {
        //进行正则匹配
        //2016-12-14
        //2016/12/14
        //20161214
        if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)){
            return new SimpleDateFormat("yyyy-MM-dd");
        }else if(Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)){
            return new SimpleDateFormat("yyyy/MM/dd");
        }else if(Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)){
            return new SimpleDateFormat("yyyyMMdd");
        }else if(Pattern.matches("^\\d{4}年\\d{2}月\\d{2}日$", source)){
            return new SimpleDateFormat("yyyy年MM月dd日");
        }
        return null;
    }
    
}

2).在配置文件中注册自定义类型转化器,将该类交由spring容器管理

 

<!-- 01.注册自定义的类型转换器 -->
    <bean id="myDateConvertion" class="com.cc8w.convertion.MyDateConvertion"></bean>
    <!--02.注册类型转换器的服务工厂  产生转换对象  -->
    <bean id="convertionservice" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.cc8w.convertion.MyDateConvertion" />
            </list>
        </property>
    </bean>
    <!--03.开启springMVC框架注解的支持,增加类型转换器,使其生效-->
    <mvc:annotation-driven conversion-service="convertionservice"/>

配置完成

------------------------------------------------

1.前端提交一个日期字符串(2022-02-02)控制器会自动用自定义的类型转换器

类型转换器验证
<form action="./index/index08" method="get"  >
    date:<input type="text" name="date" />
    <input type="submit" value="submit" />
</form>
    @RequestMapping("index08")
    public ModelAndView index08(Date date){
        System.out.println(date);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index01");
        return mv;
    }

 

 

 

转 https://www.cnblogs.com/yejiaojiao/p/6163686.html

https://blog.csdn.net/weixin_57176230/article/details/124822977

 

posted @ 2022-12-13 16:22  与f  阅读(20)  评论(0编辑  收藏  举报