SpringMVC中怎么实现一个自定义类型转换器

发布时间:2021-08-07 13:41:02 作者:Leah
来源:亿速云 阅读:121

本篇文章为大家展示了SpringMVC中怎么实现一个自定义类型转换器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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格式就会报错!

上述内容就是SpringMVC中怎么实现一个自定义类型转换器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. SpringMVC之转换器与格式化
  2. 使用SpringMVC怎么自定义一个类型转换器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springmvc

上一篇:Java8新增方法参数反射的示例分析

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》