(筆記)Spring MVC學習指南_轉換器和格式化
本章著重討論Converter和Formatter的內容。這兩者均可用於將一種物件型別轉換成另一種物件型別。Converter是通用元件,可以在應用程式的任意層中使用,而Formatter則是專門為Web層設計的。
1.Converter
為了建立Converter,必須編寫一個實現org.springframework.core.convert.converter.Converter介面的Java類。這個介面的宣告如下:
public interface Converter<S, T>
這裡的S表示源型別,T表示目標型別。
package app06a.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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);
dateFormat.setLenient(false);
return dateFormat.parse(s);
}
catch (ParseException e)
{
throw new IllegalArgumentException(
"invalid date format.Please use this pattern\"" + datePattern + "\"");
}
}
}
為了使用Spring MVC應用程式中定製的Converter,需要在Spring MVC配置檔案中編寫一個conversionService bean。Bean的類名稱必須為org.springframework.context.support.ConversionServiceFactoryBean。這個bean必須包含一個converters屬性,它將列出要在應用程式中使用的所有定製Converter。
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="app06a.converter.StringToDateConverter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy" />
</bean>
</list>
</property>
</bean>
隨後,要給annotation-driven元素的conversion-service屬性賦bean名稱:
<mvc:annotation-driven conversion-service="conversionService" />
2.Formatter
Formatter的源型別必須是一個String,而Converter則適用於任意的源型別。Formatter更適合Web層,而Converter則可以用在任意層中。
為了建立Formatter,要編寫一個實現org.springframework.format.Formatter介面的java類。下面是這個介面的宣告:
public interface Formatter<T>
這裡的T表示輸入字串要轉換的目標型別。該介面有parse和print兩個方法,所有實現都必須覆蓋。
T parse(String text, java.util.Locale locale)
String print(T object, java.util.Locale locale)
parse方法利用指定的Locale將一個String解析成目標型別。print方法與之相反,它是返回目標物件的字串表示法。
package app06b.formatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;
public class DateFormatter implements Formatter<Date>
{
private String datePattern;
private SimpleDateFormat dateFormat;
public DateFormatter(String datePattern, SimpleDateFormat dateFormat)
{
this.datePattern = datePattern;
dateFormat = new SimpleDateFormat(datePattern);
dateFormat.setLenient(false);
}
@Override
public String print(Date date, Locale locale)
{
return dateFormat.format(date);
}
@Override
public Date parse(String s, Locale locale)
throws ParseException
{
try
{
return dateFormat.parse(s);
}
catch (ParseException e)
{
// the error message will be dispalyed when using <form:errors>
throw new IllegalArgumentException(
"invalid date format.Please use this pattern\"" + datePattern + "\"");
}
}
}
為了在Spring MVC應用程式中使用Formatter,需要利用conversionService bean對它進行註冊。bean的類名稱必須為org.springframework.format.support.FormattingConversionServiceFactoryBean。
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="app06b.formatter.DateFormatter">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy" />
</bean>
</set>
</property>
</bean>
還需要給這個Formatter新增一個component-scan元素。
<context:component-scan base-package="app06b.formatter" />
3.用Registrar註冊Formatter
package app06c.formatter;
import org.springframework.format.FormatterRegistrar;
import org.springframework.format.FormatterRegistry;
public class MyFormatterRegistrar implements FormatterRegistrar
{
private String datePattern;
public MyFormatterRegistrar(String datePattern)
{
this.datePattern = datePattern;
}
@Override
public void registerFormatters(FormatterRegistry registry)
{
registry.addFormatter(new DateFormatter(datePattern));
// register more formatters here
}
}
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatterRegistrars">
<set>
<bean class="app06c.formatter.MyFormatterRegistrar">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy" />
</bean>
</set>
</property>
</bean>