package com.hope.utils;

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

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
第一步定义一个类型转换器的类
* @author newcityman
* @date 2019/11/26 - 19:57
*/
public class StringToDateConverter implements Converter<String,Date>{

/**
*
* @param source
* @return
*/
@Override
public Date convert(String source) {
if(StringUtils.isEmpty(source)){
throw new RuntimeException("参数不能为空,请输入一个参数。");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(source);
} catch (ParseException e) {
throw new RuntimeException("日期转换失败,请联系管理员。");
}
}
}

// 第二步,在springmvc.xml中配置自定义类型转换器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/context/spring-context.xsd">

<!--指定spring扫描的包-->
<context:component-scan base-package="com.hope"></context:component-scan>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.hope.utils.StringToDateConverter"></bean>
</set>
</property>
</bean>

<!--开启springmvc框架注解的支持,包含开启处理器映射器和处理器适配器-->
<mvc:annotation-driven conversion-service="conversionService"/>
</beans>
 

最新文章

  1. Android 获取系统相册中的所有图片
  2. Linux下安装JDK并配置环境变量
  3. bash coding to changeNames
  4. paip.java gui swt/jface 最佳实践
  5. 一、Stream,sink,source,transform
  6. easyui datagrid 行右键生成 动态获取(toolbar) 按钮
  7. A手机等的网络udp广播,收到广播以后回复udp消息
  8. 史上最全的 UIWebview 的 JS 与 OC 交互
  9. HTML控件ID和NAME属性的区别,以及如何在asp.net页面的.CS文件中获得.ASPX页面中HTML控件的值
  10. Maven笔记(二)仓库
  11. Android滑动菜单框架完全解析,教你如何一分钟实现滑动菜单特效
  12. win10 uwp 读写XML
  13. Warning: count(): Parameter must be an array or an object that implements Countable in line 302解决方法
  14. python cookbook 笔记三
  15. UML和模式应用4:初始阶段(7)--其它需求
  16. docker使用dockerfile 构建redis镜像
  17. 《Lua程序设计》第5章 函数 学习笔记
  18. html &lt;form&gt;相关表单
  19. 洛谷 P1446 [HNOI2008]Cards 解题报告
  20. Extjs NumberField 开始值 不能大于 结束值

热门文章

  1. hudi clustering 数据聚集(三 zorder使用)
  2. k8s网络模型与集群通信
  3. PAT A1103—DFS
  4. [cf1305G]Kuroni and Antihype
  5. [loj6500]操作
  6. Java设计模式之(六)——桥接模式
  7. Plugin [id: &#39;org.jetbrains.kotlin.jvm&#39;] was not found in any of the following sources: gradle配置:kotlin(&quot;jvm&quot;)后报错
  8. .net打独立运行环境遇到无法trim遇到的bug
  9. Codeforces 1304F1/F2 Animal Observation(单调队列优化 dp)
  10. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)