SpringMVC自定义类型转换器实现解析

 更新时间:2019年12月05日 14:54:01   作者:1572662  
这篇文章主要介绍了SpringMVC自定义类型转换器实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了SpringMVC自定义类型转换器实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

页面录入的字符串: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格式就会报错!

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • RedisTemplate中opsForValue和opsForList方法的使用详解

    RedisTemplate中opsForValue和opsForList方法的使用详解

    这篇文章主要介绍了RedisTemplate中opsForValue和opsForList方法的使用详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Spring@Value使用获取配置信息为null的操作

    Spring@Value使用获取配置信息为null的操作

    这篇文章主要介绍了Spring@Value使用获取配置信息为null的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java开发反射机制的实战经验总结

    Java开发反射机制的实战经验总结

    反射是java中一种强大的工具,能够使我们很方便的创建灵活的代码,这些代码可以再运行时装配,无需在组件之间进行源代码链接,但是反射使用不当会成本很高,这篇文章主要给大家介绍了关于Java开发反射机制的相关资料,需要的朋友可以参考下
    2021-07-07
  • 轻松掌握java外观模式

    轻松掌握java外观模式

    这篇文章主要帮助大家轻松掌握java外观模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Spring Security permitAll()不允许匿名访问的操作

    Spring Security permitAll()不允许匿名访问的操作

    这篇文章主要介绍了Spring Security permitAll()不允许匿名访问的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • IDEA自定义Maven archetype的方法步骤

    IDEA自定义Maven archetype的方法步骤

    在创建Maven的项目时我们发现了一个很不方便的问题,就是每次创建Maven的工程的时候,都需要选择一个骨架,本文主要介绍了IDEA自定义Maven archetype的方法步骤,感兴趣的可以了解一下
    2022-03-03
  • Java使用Filter实现登录验证

    Java使用Filter实现登录验证

    本文主要介绍了Java使用Filter实现登录验证,Filter类似于门卫,你在进入之前门卫需要盘查你,身份合法进入,身份不合法拦截,感兴趣的可以了解一下
    2023-11-11
  • java之使用多线程代替for循环(解决主线程提前结束问题)

    java之使用多线程代替for循环(解决主线程提前结束问题)

    这篇文章主要介绍了java之使用多线程代替for循环(解决主线程提前结束问题),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Java实现获取Excel中的表单控件

    Java实现获取Excel中的表单控件

    Excel中可通过【开发工具】菜单栏下插入表单控件,如文本框、单选按钮、复选框、组合框等等。本文将利用Java实现获取Excel中的表单控件,需要的可以参考一下
    2022-05-05
  • Java List的remove()方法陷阱以及性能优化

    Java List的remove()方法陷阱以及性能优化

    Java List在进行remove()方法是通常容易踩坑,本文就详细的介绍一下陷阱以及性能优化,感兴趣的可以了解一下
    2021-10-10

最新评论