温馨提示×

温馨提示×

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

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

使用SpringMVC怎么自定义一个类型转换器

发布时间:2021-04-17 16:19:08 来源:亿速云 阅读:93 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关使用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>

关于使用SpringMVC怎么自定义一个类型转换器就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

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

AI