多平台统一管理软件接口,如何实现多平台统一管理软件接口
361
2022-12-20
SpringMVC自定义类型转换器实现解析
这篇文章主要介绍了SpringMVC自定义类型转换器实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
页面录入的字符串:2019/12/05可以映射到实体的日期属性上,但是如果是录入2019-12-05就会报错400 http://bad request,想要以2019-12-05日期格式的方式映射到实体的日期属性上,需要自定义类型转换器,主要步骤如下:
1、 自定义类实现Convertro接口
2、Springmvc.xml中配置ConversionServiceFactoryBean,其属性上配置我们自定义的转换器
3、欲使配置的转换器生效,需要将springmvc.xml的
1、 自定义类实现Convertro接口
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
@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,其属性上配置我们自定义的转换器
3、欲使配置的转换器生效,需要将springmvc.xml的
springmvc.xml的完整配置如下:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop" xmlns:c="http://springframework.org/schema/c" xmlns:cache="http://springframework.org/schema/cache" xmlns:context="http://springframework.org/schema/context" xmlns:jdbc="http://springframework.org/schema/jdbc" xmlns:jee="http://springframework.org/schema/jee" xmlns:lang="http://springframework.org/schema/lang" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:p="http://springframework.org/schema/p" xmlns:task="http://springframework.org/schema/task" xmlns:tx="http://springframework.org/schema/tx" xmlns:util="http://springframework.org/schema/util" xsi:schemaLocation="http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee-4.3.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.3.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.3.xsd http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-4.3.xsd http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.3.xsd http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-4.3.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd ">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:c="http://springframework.org/schema/c"
xmlns:cache="http://springframework.org/schema/cache"
xmlns:context="http://springframework.org/schema/context"
xmlns:jdbc="http://springframework.org/schema/jdbc"
xmlns:jee="http://springframework.org/schema/jee"
xmlns:lang="http://springframework.org/schema/lang"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:p="http://springframework.org/schema/p"
xmlns:task="http://springframework.org/schema/task"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:util="http://springframework.org/schema/util"
xsi:schemaLocation="http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee-4.3.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd
http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.3.xsd
http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-4.3.xsd
http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-4.3.xsd
http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-4.3.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.3.xsd ">
注意:自定义的类型转换器生效之后,日期格式就只能使用yyyy-MM-dd的格式了,若再使用原有的yyyy/MM/dd格式就会报错!
如有理解不到之处,望指正!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~