1. 程式人生 > 程式設計 >SpringMVC自定義型別轉換器實現解析

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格式就會報錯!

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。