• springmvc类型转换器


    package com.orange.converter;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.regex.Pattern;
    
    import org.springframework.core.convert.converter.Converter;
    
    public class MyDateConverter implements Converter<String, Date> {
    
        public Date convert(String source) {
            
            try {
                SimpleDateFormat sdf = getSimpleDateFormat(source);
                return sdf.parse(source);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            
            return null;
        }
        
        private SimpleDateFormat getSimpleDateFormat(String source){
            SimpleDateFormat sdf = null;
            
            if(Pattern.matches("^\d{4}-\d{2}-\d{2}$", source)){
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            }else if(Pattern.matches("^\d{4}\d{2}\d{2}$", source)){
                sdf = new SimpleDateFormat("yyyyMMdd");
            }else if(Pattern.matches("^\d{4}/\d{2}/\d{2}$", source)){
                sdf = new SimpleDateFormat("yyyy/MM/dd");
            }
            
            return sdf;
        }
    
    }
    复制代码

    注册类型转换器,配置spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="  
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 扫描注解 -->
        <context:component-scan base-package="com.orange.controller" />    
        
        <!-- 开启类型转换服务 -->
        <mvc:annotation-driven conversion-service="conversionService"/>
        
        <!-- 注册自定义类型转换器 -->
        <bean id="dateConverter" class="com.orange.converter.MyDateConverter"></bean>
        
        <!-- 注册类型转换服务 -->
        <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters" ref="dateConverter"></property>
        </bean>
        
    </beans>
  • 相关阅读:
    docker tcp配置
    PostgreSQL 数据库备份
    docker 几种磁盘卷挂载方式的区别
    MAT
    OkHttp
    HashMap 在 Java1.7 与 1.8 中的区别
    【zabbix】zabbix 高可用架构的实现
    利用zabbix监控Vmware运行
    logback在SpringBoot下出现no applicable action for [appender], current ElementPath is
    NGUI:HUD Text(头顶伤害漂浮文字)
  • 原文地址:https://www.cnblogs.com/wdas-87895/p/6770534.html
Copyright © 2020-2023  润新知