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>
 

springmvc中如何自定义类型转换器的更多相关文章

  1. springmvc:自定义类型转换器代码编写

    字符串转换日期: 1.自定义一个类 /** * 字符串转换日期 */ public class StringToDateConverter implements Converter<String ...

  2. 《SpringMVC从入门到放肆》十二、SpringMVC自定义类型转换器

    之前的教程,我们都已经学会了如何使用Spring MVC来进行开发,掌握了基本的开发方法,返回不同类型的结果也有了一定的了解,包括返回ModelAndView.返回List.Map等等,这里就包含了传 ...

  3. springmvc 类型转换器 自定义类型转换器

    自定义类型转换器的步骤: 1.定义类型转换器 2.类型转换器的注册(在springmvc配置文件处理) 来解决多种日期格式的问题: springmvc 类型转换器 表单数据填错后返回表单页面(接上面的 ...

  4. SpringMVC自定义类型转换器

    SpringMVC 自定义类型转换器  我们在使用SpringMVC时,常常需要把表单中的参数映射到我们对象的属性中,我们可以在默认的spring-servlet.xml加上如下的配置即可做到普通数据 ...

  5. springmvc——自定义类型转换器

    一.什么是springmvc类型转换器? 在我们的ssm框架中,前端传递过来的参数都是字符串,在controller层接收参数的时候springmvc能够帮我们将大部分字符串类型的参数自动转换为我们指 ...

  6. 阶段3 3.SpringMVC·_02.参数绑定及自定义类型转换_6 自定义类型转换器代码编写

    mvc是基于组件的方式 类型转换的接口Converter,想实现类型转换,必须实现这个接口 Ctrl+N搜索 converter 这是一个接口类 它有很多的实现类.S是字符串.后面T是指要转换类型 新 ...

  7. 自定义类型转换器 及 使用 ServletAPI 对象作为方法参数

    自定义类型转换器使用场景: jsp 代码:  <!-- 特殊情况之:类型转换问题 --> <a href="account/deleteAccount?date=2018- ...

  8. 0023SpringMVC自定义类型转换器

    页面录入的字符串:2019/12/05可以映射到实体的日期属性上,但是如果是录入2019-12-05就会报错400 bad request,想要以2019-12-05日期格式的方式映射到实体的日期属性 ...

  9. SpringMVC学习笔记六:类型转换器及类型转换异常处理

    SpringMVC内部有类型转换器,当从Request中获取参数后,放入Controller中时,会根据Controller中指定的类型进行自动转换,当指的类型SpringMVC不能自动转换时,就需要 ...

随机推荐

  1. WPF进阶技巧和实战09-事件(1-路由事件、鼠标键盘输入)

    理解路由事件 当有意义的事情发生时,有对象(WPF的元素)发送的用于通知代码的消息,就是事件的核心思想.WPF通过事件路由的概念增强了.NET事件模型.事件由允许源自某个元素的事件由另一个元素引发.例 ...

  2. Java设计模式之(三)——建造者模式

    1.什么是建造者模式 Separate the construction of a complex object from its representation so that the same co ...

  3. 看动画学算法之:hashtable

    目录 简介 散列表的关键概念 数组和散列表 数组的问题 hash的问题 线性探测 二次探测 双倍散列 分离链接 rehash 简介 java中和hash相关并且常用的有两个类hashTable和has ...

  4. springboot配置自动提示插件-IDEA

    社区版的IDEA编辑spring boot项目的properties或者yml文件不会自动提醒.可手动安装IDEA的插件解决. Setting >> Plugins >> 搜索 ...

  5. 【CTSC1999】【带权并查集 】月亮之眼

    Description 吉儿是一家古董店的老板娘,由于她经营有道,小店开得红红火火.昨天,吉儿无意之中得到了散落民间几百年的珍宝-月亮之眼.吉儿深知"月亮之眼"价值连城:它是由许多 ...

  6. 题解 P5320 - [BJOI2019]勘破神机(推式子+第一类斯特林数)

    洛谷题面传送门 神仙题(为什么就没能自己想出来呢/zk/zk) 这是我 AC 的第 \(2\times 10^3\) 道题哦 首先考虑 \(m=2\) 的情况,我们首先可以想到一个非常 trivial ...

  7. R语言与医学统计图形-【26】ggplot2主题函数

    ggplot2绘图系统--主题函数 1. theme函数 theme_*系列函数提供了9种不同的风格. theme_grey/gray/bw/linedraw/light/minimal/classi ...

  8. python15正则表达式

    ------------恢复内容开始------------ python实现实现实现实现 import re #将表达式编译,返回一个对象, pattern = re.compile(r" ...

  9. Oracle-left join两表关联只取B表匹配到的第一条记录【over partition by(分组后对组内数据排序)】

    背景:  A表.B表两表关联,关联出来的结果里B表有不止一条,需求是只要B表结果中的某一条(按某字段排序) 经过百度,发现 row_number() over(partition by a order ...

  10. LeetCode 第一题 两数之和

    题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...