页面录入的字符串:2019/12/05可以映射到实体的日期属性上,但是如果是录入2019-12-05就会报错400 bad request,想要以2019-12-05日期格式的方式映射到实体的日期属性上,需要自定义类型转换器,主要步骤如下:

1、  自定义类实现Convertro<S,T>接口

2、Springmvc.xml中配置ConversionServiceFactoryBean,其属性上配置我们自定义的转换器

3、欲使配置的转换器生效,需要将springmvc.xml的<mvc:annotation-driven />改为

<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>

1、  自定义类实现Convertro<S,T>接口

package com.example.util;

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; public class StingToDateConvertr implements Converter<String, Date> {
@Override
public Date convert(String s) {
if(StringUtils.isEmpty(s)){
throw new RuntimeException("日期字符串不能为空!");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(s);
} catch (ParseException e) {
throw new RuntimeException("类型转换出错!");
}
}
}

2、Springmvc.xml中配置ConversionServiceFactoryBean,其属性上配置我们自定义的转换器

<!--配置自定义类型转换器-->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.example.util.StingToDateConvertr" />
</set>
</property>
</bean>

3、欲使配置的转换器生效,需要将springmvc.xml的<mvc:annotation-driven />改为

<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>

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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<!--开启注解扫描-->
<context:component-scan base-package="com.example" />
<!--视图解析器,根据Controller返回的字符串找对应的文件-->
<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="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.example.util.StingToDateConvertr" />
</set>
</property>
</bean> <!--1、开启springmvc框架注解的支持-->
<!--2、欲使配置的自定义类型转换器生效,需加上conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/> </beans>
注意:自定义的类型转换器生效之后,日期格式就只能使用yyyy-MM-dd的格式了,若再使用原有的yyyy/MM/dd格式就会报错!

如有理解不到之处,望指正!
 

0023SpringMVC自定义类型转换器的更多相关文章

  1. Struts2框架的自定义类型转换器

    前言:对于java的基本数据类型及一些系统类(如Date类.集合类),Struts2提供了内置类型转换功能,但是也有一定的限制.所以就演示出自定义类型转换器 一.应用于局部类型转换器 eg.用户登录出 ...

  2. sruts2 自定义类型转换器

    1.1.1    Struts2中自定义类型转换器:(了解) 类型转换的过程是双向的过程: JSP---->Action参数提交:String---Date. Action---->JSP ...

  3. Struts2之自定义类型转换器

    Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用d ...

  4. [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. 自定义类型转换器converter

    作用:目前将日期转换成string,将string转换成我想要的类型   0509课件里讲 一.数据类型转换在web应用程序中,数据存在两个方向上的转换:1.当提交表单时  表单数据以字符串的形式提交 ...

  6. Ⅵ.spring的点点滴滴--自定义类型转换器

    承接上文 自定义类型转换器 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class CustomeConverter : TypeConverter{ ...

  7. struts2自定义类型转换器

    首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机 ...

  8. Struts(二十):自定义类型转换器

    如何自定义类型转换器: 1)为什么需要自定义类型转化器?strtuts2不能自动完成字符串到所有的类型: 2) 如何定义类型转化器? 步骤一:创建自定义类型转化器的类,并继承org.apache.st ...

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

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

随机推荐

  1. window10 phpstudy2018 mysql服务重启之后自动停止

    使用phpstudy集成环境开发php,但是可能版本太旧,导致有些语法用不了.所以决定删掉,再下一个新版的. 把phpstudy退出之后,就直接把phpstudy文件夹删除了.发现它并不能删除成功.然 ...

  2. 【视频开发】【电子电路技术】监控球机PTZ的功能介绍

    主要分模拟球机和网络球机两种 1.模拟球机 模拟球机除了需要接电源外,还需要接视频线和485控制线才能实现视频传输和云台控制,并且需要配置波特率,地址位和协议. 升级版:HDTVI球机 ① 同轴视控. ...

  3. Oracle Spatial分区应用研究之二:按县分区与按省分区对比测试报告

    1.实验目的 在上一轮的实验中,oracle 11g r2版本下,在87县市实验数据的基础上,比较了分表与分区的效率,得出了分区+全局索引效率较高的结论(见上一篇博客).不过我们尚未比较过不同的分区粒 ...

  4. Netty 基本原理

    转载. https://blog.csdn.net/qq_27641935/article/details/86543578 之前在看rocketmq源码时,发现底层用了Netty,顺便学习了一下,网 ...

  5. 关于MySQL的内连接,外连接的总结

    首先创建两个数据库表,用户表u,账户表a 先看一下数据库表: u表: a表: 1. 内连接.    关键字:inner join SQL语句: select u.*,a.ID as aid,a.mon ...

  6. Spring Boot之从Spring Framework装配掌握SpringBoot自动装配

    Spring Framework模式注解 模式注解是一种用于声明在应用中扮演“组件”角色的注解.如 Spring Framework 中的 @Repository 标注在任何类上 ,用于扮演仓储角色的 ...

  7. 分布式全文搜索引擎(ElasticSearch)

    1. ElasticSearch介绍(简称ES) ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案. 首先,ES的索引库管理支持依然是基于Ap ...

  8. Json 文件读写以及和IniFile 转换

    JSON 文件是越来越受欢迎了,以前程序配置文件用Ini,Ini 简练,简单.方便,但是也有不少缺点,比如,没有 JSON 直观,无法存储复杂点的数据类型. 于是乎,我封装了一个TJsonFile 的 ...

  9. Django框架4——form表单

    HTML表单一直是交互性网站的支柱,使用form组件对用户通过表单提交的数据进行访问.有效性检查以及其他处理 从Request对象中获取数据 URL相关信息 属性/方法 说明 举例 request.p ...

  10. centos将nginx服务设置为开机自动启动

    centos将nginx服务设置为开机自动启动 1.在/etc/init.d下创建文件nginx 发现原来就有并且配置就是默认的,而且是正确的 命令为: vim /etc/init.d/nginx 需 ...