0023SpringMVC自定义类型转换器

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

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

原文地址:https://www.cnblogs.com/xiao1572662/p/11988489.html

时间: 2024-07-31 00:54:02

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

springmvc 类型转换器 自定义类型转换器

自定义类型转换器的步骤: 1.定义类型转换器 2.类型转换器的注册(在springmvc配置文件处理) 来解决多种日期格式的问题:

struts2基础----&gt;自定义类型转换器

这一章,我们开始struts2中自定义类型转换器的学习. 自定义类型转换器 一.定义一个继承于StrutsTypeConverter的转换类: package com.huhx.converter; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import o

sruts2 自定义类型转换器

1.1.1    Struts2中自定义类型转换器:(了解) 类型转换的过程是双向的过程: JSP---->Action参数提交:String---Date. Action---->JSP数据回显:Date---String. 自定义类型转换器: * 1.实现TypeConverter * convertValue(java.util.Map<java.lang.String,java.lang.Object> context, java.lang.Object target, j

struts2自定义类型转换器

首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机制即可满足大部分类型转换需求.如: 类User.java package models; public class User { private String username; private String password; public String getUsername() { retur

Struts2 自定义类型转换器

Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性 用date类型是可以接收到的,但是如果传的是20101112这样类型的字符串,用date类型是获取不到,并且会出现错误的,struts2提供了一 种类型转换器供我们使用. 以下为局部类型转换器的开发步骤a.首先要写一个类来继承DefaultTypeConverterb.然后覆盖convertValue

自定义类型转换器converter

作用:目前将日期转换成string,将string转换成我想要的类型   0509课件里讲 一.数据类型转换在web应用程序中,数据存在两个方向上的转换:1.当提交表单时  表单数据以字符串的形式提交给服务器,在服务器端转换成不同的Java数据类型 2.当需要在页面显示数据时  将不同的Java数据类型转换为对应的字符串格式显示输出 二.Struts2内置的类型转换Struts2内置了常用类型的自动转换功能,支持常见数据类型与字符串之间的转换 三.自定义类型转换1.编写自定义类型转换器,继承St

strut2 自定义类型转换器

描述:假设用户请求地址:http://xxx.action?date=2015-01-01.  假设action中已经存在了date属性,且类型为Date.当用户请求的参数格式为xxxx-xx-xx或者xxxx-x-x的时候,action可以正常的接收到日期类型,即string字符串可以自动转换为Date类型.但是当用户的请求为xxxxxxxx(20150101)的时候,action不能够转换为日期类型,且报错.这时候就需要自定义类型转换器来实现日期的转换. 问题解决:自定义类型转换器 新建立一

springmvc 自定义类型转换器

springmvc除了自带的部分类型转换之外,还可以自定义类型转换器,按照以下步骤: 1.写一个类实现Converter接口 package com.hy.springmvc.entities; import org.springframework.core.convert.converter.Converter; import com.google.gson.Gson; public class DepartmentConvertor implements Converter<String,

自定义类型转换器

1 一.自定义类型转换器 2 1.编写一个类,继承com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter 3 2.覆盖掉其中的public Object convertValue(Map<String, Object> context, Object value,Class toType) 4 context:OGNL表达式的上下文 5 value:实际的值.用户输入的都是字符串,但他是一个String数组. 6 toType: