spring mvc jsp傳遞參數到controller涉及到日期類型數據,需要使用到類型轉換器:
目前筆者找到兩種類型轉換器可以使用:
類型一:實現Convert<Source,Target>接口的方式(Source源數據,Target目標數據類型),實現功能是一種數據類型到另一種數據類型:
數據轉換類如下:在不添加DateTimeFormatter.ofPattern("yyyy/MM/dd")時(MM必須大寫,小寫表示時間分),默認需要輸入的String樣式“yyyy-MM-dd”
public class StringToLocalDate implements Converter<String,LocalDate> //String轉換為LocalDate
{
@Override
public LocalDate convert(String s)
{
return LocalDate.parse(s,DateTimeFormatter.ofPattern("yyyy/MM/dd"));
}
}
spring-servlet.xml配置如下:在mvc:annoation-driven內部conversion-service默認裝配的bean為FormattingConversionServiceFactoryBean,所以在使用convert時需要顯示的設置該conversion-service對應的bean為ConversionServiceFactoryBean
<mvc:annotation-driven conversion-service="conversionService" />
<!--注冊自定義數據類型轉換器工廠,並添加自定義的轉換器bean-->
<bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="conversionService">
<property name="converters">
<set>
<bean class="com.vastliu.DataConvert.StringToLocalDate"/> //StringToLocalDate是自定義的類型轉換器
</set>
</property>
</bean>
到此借用convert實現自定義轉換器,需要實現的接口以及需要配置的地方全部完成,可以將Source類型轉換成Target類型了。此處筆者使用的是String轉換成LocalDate作為示例;
類型二:使用FormattingConversionServiceFactoryBean格式化轉換,此處可以使用注解簡單的轉換,也可以使用自定義類方法轉換
注解轉換:
需要在Pojo實體的屬性上使用注解,筆者使用LocalDate做示例,故需要在數據類型為LocalDate的變量上添加注解如下:
import org.springframework.format.annotation.DateTimeFormat; //導入該注解對應的包
@DateTimeFormat(pattern="yyyy-MM-dd") //pattern是設置傳入String的樣式類型,非此樣式類型的String不能轉換成目標類型LocalDate
private LocalDate releaseDate; //發布日期
由於mvc:annoation-driven內部默認就是FormattingConversionServiceFactoryBean該工廠,所以無需顯示的設置conversion-service,
<mvc:annotation-driven />
建議還是顯示的設置一下
<mvc:annotation-driven conversion-service="conversionService2" />
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService2"/>
至此注解轉換需要完成的配置等已完成;
自定義類轉換:
需要創建一個自定義轉換類並實現Formatter<T>接口,如下:
public class LocalDateFormatterForDash implements Formatter<LocalDate>
{
/*聲明一個日期時間格式化的屬性*/
private DateTimeFormatter dateTimeFormatter;
/*聲明一個字符串作為格式化的樣式*/
private String datePattern;
/*創建構造器用以初始化時獲取需要轉換的String樣式datePattern,並根據該datePattern樣式生成一個DateTimeFormatter對象*/
public LocalDateFormatterForDash(String datePattern)
{
this.datePattern=datePattern;
dateTimeFormatter=DateTimeFormatter.ofPattern(datePattern);
}
/*重寫接口提供的方法,將源字符串數據source轉換成目標數據類型LocalDate,local是本地化*/
@Override
public LocalDate parse(String source, Locale locale) throws ParseException
{
/*將符合datePattern樣式的String轉化為LocalDate並返回*/
LocalDate localDate=LocalDate.parse(source,dateTimeFormatter);
return localDate;
}
/*將LocalDate轉換成字符串*/
@Override
public String print(LocalDate localDate, Locale locale)
{
return localDate.format(dateTimeFormatter);
}
}
自定義轉換器類以及聲明並編輯完成,接下來需要讓spring來讓其發揮作用:
有兩種方法:
一是將該類直接添加到FormattingConversionServiceFactoryBean中如下:
<mvc:annotation-driven conversion-service="conversionService" />
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
<!--添加自定義formatter到formatter倉庫中-->
<property name="formatters">
<set>
<bean class="com.vastliu.DataConvert.LocalDateFormatterForDash">
<!--構造器注入String樣式yyyy/MM/dd-->
<constructor-arg name="datePattern" value="yyyy/MM/dd"/>
</bean>
</set>
</property>
</bean>
到此直接添加方式已完成相關配置;
二是將該類添加到MyFormatterRegistrar注冊類中,在該類中完成注冊
MyFormatterRegistrar注冊類實現FormatterRegistrar該接口
public class MyFormatterRegistrar implements FormatterRegistrar
{
/*聲明需要轉換String的樣式*/
private String dataPatternForDash;
/*構造器*/
public MyFormatterRegistrar(String dataPatternForDash)
{
this.dataPatternForDash=dataPatternForDash;
}
@Override
public void registerFormatters(FormatterRegistry formatterRegistry)
{
/*將創建的自定義類轉換器注冊(添加)到注冊類中,完成注冊(以注冊類中的樣式)*/
formatterRegistry.addFormatter(new LocalDateFormatterForDash(dataPatternForDash));
}
}
xml配置:
<mvc:annotation-driven conversion-service="conversionService" />
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
<!--將注冊類添加到FormattingConversionServiceFactoryBean-->
<property name="formatterRegistrars">
<set>
<!--初始化自定義注冊類時,並傳入String樣式-->
<bean class="com.vastliu.DataConvert.MyFormatterRegistrar">
<constructor-arg name="dataPatternForDash" value="yyyy-MM-dd"/>
</bean>
</set>
</property>
</bean>
到此借助注冊類的方式已經完成相關配置;想分享的數據類型轉換的幾種方式已經分享完畢,如有紕漏望指正;
ps:自定義轉換器針對同一種目標數據類型(如:針對LocalDate自定義了多個轉換器)spring默認會使用xml配置中的最后一個轉換器執行轉換操作。
