功能:
Spring框架的功能,Formatter和Converter均可以将一种对象类型转换成另一种对象类型。
区别:
Converter是通用元件,可以在应用程序的任意层中使用。
Formatter是专门为Web层设计的。
Formatter的原类型必须是String,Converter适用于任意类型。
Converter的使用:
1、实现org.springframework.core.convert.converter.Converter接口
接口的声明:public interface Converter<S,T> (S是原类型,T是目标类型)
实现T conver (S source)类
2、配置springmvc配置文件
编写一个conversion bean ,类名为:org.springframework.context.support.ConversionServiceFactoryBean
bean包含一个converters属性,列出要在应用程序中使用的Converter。
3、添加<mvc:annotation-driven conversion-service="上边那个bean的名称"/>
Converter的实例(将String类型的日期转为Date):
import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.core.convert.converter.Converter; import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException; public class StringToDateConverter implements Converter<String, Date> { private String datePattern; public StringToDateConverter(String datePattern) { this.datePattern = datePattern; System.out.println("converter with pattern:"+datePattern); } @Override public Date convert(String s) { try { SimpleDateFormat sdf = new SimpleDateFormat(datePattern); sdf.setLenient(false);//是否严格解析日期 return sdf.parse(s); } catch(java.text.ParseException e) { throw new IllegalArgumentException( "Invalid date format. Please use this pattern\"" + datePattern + "\""); } } }
springmvc配置文件:
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="converter.StringToDateConverter"> <constructor-arg type="java.lang.String" value="MM-dd-yyyy"> </constructor-arg> </bean> </list> </property> </bean>
Formatter的使用:
1、实现org.springframework.format.Formatter接口
接口的声明:public interface Formatter<T> (T是目标类型)
实现
T parse(String text, java.util.Locale locale) String print(T object, java.util.Locale locale)
parse方法利用指定的locale将String解析成目标类型。
print方法返回目标对象的字符串表示法。
2、配置springmvc配置文件
编写一个conversionService bean ,类名为:org.springframework.format.support.FormattingConversionServiceFactoryBean
3、添加<mvc:annotation-driven conversion-service="上边那个bean的名称"/>
Formatter的实例(将String类型的日期转为Date):
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 sdf; public DateFormatter(String datePattern) { this.datePattern = datePattern; sdf = new SimpleDateFormat(datePattern); sdf.setLenient(false); } @Override public String print(Date date, Locale locale) { return sdf.format(date); } @Override public Date parse(String s, Locale locale) throws ParseException { try { return sdf.parse(s); } catch(ParseException e) { throw new IllegalArgumentException( "Invalid date fromat. Please use this pattern\""+datePattern+"\""); } } }
springmvc配置文件:
<context:component-scan base-package="formatter"></context:component-scan> //配置包扫描器
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="formatters"> <set> <bean class="formatter.DateFormatter"> <constructor-arg type="java.lang.String" value="MM-dd-yyyy"> </constructor-arg> </bean> </set> </property> </bean>