(メモ)Spring MVC学習マニュアル_変換器とフォーマット
11742 ワード
この章はCoverterとFormaterの内容を重点的に討論します.この2つは、オブジェクトタイプを別のオブジェクトタイプに変換するために使用できます.Coverterは汎用部品で、アプリケーションの任意層で使用できます.FormaterはWeb層のために設計されています.1.onverterは、Coverterを作成するために、org.springframe ebook.co.re.com nverter.C.onverterインターフェースを実現するJavaクラスを作成しなければならない.このインターフェースの宣言は以下の通りである.
public interface Converter
ここの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アプリケーションでカスタマイズしたCoverterを使用するためには、Spring MVCプロファイルにconversion Service beanを作成する必要があります.Beaビンのクラス名はorg.springframe ebork.com ntect.support.Conversion ServiceFactoryBenでなければなりません.このbeanは一つのconverters属性を含んでいる必要があります.アプリケーションで使用するすべてのカスタマイズCoverterがリストされます. <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>
その後、annotationn-driven要素のconversion-service属性にbeanの名前を付けます.<mvc:annotation-driven conversion-service="conversionService" />
2.Formater FormaterのソースタイプはStringでなければならないが、Coverterは任意のソースタイプに適用される.FormaterはWeb層に適していますが、Coverterは任意の層に使えます.Formaterを作成するために、org.springframewark.format.Formaterインターフェースを実現するjavaクラスを作成します.以下はこのインターフェースの宣言である.public interface Formatter
ここのTは入力文字列が変換される対象のタイプを示す.このインターフェースには、パースとprintの2つの方法があり、すべての実装がカバーされなければならない.T parse(String text、java.util.Locall local e)String print(T object、java.util.Locall 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
throw new IllegalArgumentException(
"invalid date format.Please use this pattern\"" + datePattern + "\"");
}
}
}
Spring MVCアプリケーションでFormaterを使用するためには、conversion Service beanを利用して登録する必要があります.beanのクラス名はorg.spring frame ebook.format.support.Formating Coversion ServiceFactoryBenでなければなりません. <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>
このFormaterにcomponent-scan要素を追加します. <context:component-scan base-package="app06b.formatter" />
3.RegistarでFormaterを登録する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>