天天看點

Java EE - Spring MVC 資料格式控制(重點:字元串與日期的轉換)

問題導讀:

1. Converter 轉換器

2. Formatter 格式化

解決方案:

Converter 轉換器

代碼托管:​​https://github.com/LiPenglin/MySpringMVC/tree/master/SpringMVCDemo3​​

流程

  1. 建立屬于自己的converter 類,這個converter 類需要實作 org.springframework.core.convert.converter.Converter 泛型接口,注意源類型和目标類型
  2. 在Spring MVC 配置檔案中配置好 ConversionServiceFactoryBean

實作

主要代碼

package Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
 * 建立Converter
 * 1. 實作 org.springframework.core.convert.converter.Converter 接口
 * 2. Converter<S, T> S -> 源類型 T -> 目标類型
 */
import org.springframework.core.convert.converter.Converter;

public class StringToDateConverter implements Converter<String, Date>{

  // 日期格式
  private String datePattern;
  
  public StringToDateConverter(String datePattern) {
    this.datePattern = datePattern;
  }
  
  @Override
  public Date convert(String s) {
    try {
      SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
      // 如果日期不合格則需要抛出異常,不能正常計算,是以要禁止SimpleDateFormat的自動計算功能
      dateFormat.setLenient(false);
      return dateFormat.parse(s);
    } catch (ParseException e) {
      throw new IllegalArgumentException("非法參數異常,時期格式錯誤!");
    }
  }
  
}      
<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
           <!-- 掃描包 -->    
           <context:component-scan base-package="controller" />
           <!-- 視圖解析器 -->
           <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
           </bean>
           <!-- 
              配置 conversionService bean
              1. bean 名稱:org.springframework.context.support.ConversionServiceFactoryBean
              2. bean 必備屬性:converters 列出所有定制的Converter
            -->
           <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
              <list>
                <bean class="Converter.StringToDateConverter">
                  <constructor-arg type="java.lang.String" value="yyyy/MM/dd" />
                </bean>
              </list>
            </property>
           </bean>
           <mvc:annotation-driven conversion-service="conversionService"/> 
</beans>      

測試

正确格式

Java EE - Spring MVC 資料格式控制(重點:字元串與日期的轉換)
Java EE - Spring MVC 資料格式控制(重點:字元串與日期的轉換)

錯誤格式

Java EE - Spring MVC 資料格式控制(重點:字元串與日期的轉換)

Formater 格式化

  1. 實作類似converter,詳細參考​​《Spring MVC 學習指南》​​第六章
  2. Converter 可以将随便一種類型轉換成另一種類型,可以應用在web層也可以在其他層
  3. Formatter 更适用于web 層,隻能将String 轉成 Java 類型