Converter
Spring的Converter是可以将一种类型转换成另一种类型的一个对象。例如,用户输入的日期可能有许多形式,如“December 25,2014”,”12/25/2014”和“214-12-25”,这些都表示同一个日期。默认情况下,Spring会期待用户输入的日期样式与当前语言区域的日期样式相同。
为了创建Converter,必须编写一个实现org.springframework.core.converter.Converter接口的Java类。这个接口声明如下:
public interface Converter<S, T>
这里的S表示源类型,T表示目标类型。
例如,为了创建一个可以将Long转换成Date的Converter,要像下面这样声明Converter类:
```
在类body中,需要编写一个来自Converter接口的conver方法实现。这个方法的签名如下:
``` T convert(S source) ```
样式代码
public class StringToDateConverter implements Converter<String, Date>{
private String datePattern;//一种自己需要的固定的格式
//无参的构造函数
public StringToDateConverter(String datePattern) {
this.datePattern = datePattern;
System.out.println("instantiating .... converter with pattern:*" + 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.COncersionServiceFactoryBean.这个Bean必须包含一个COnverters属性,他将列出要在应用程序中使用所有定制Converter.
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="controller"></context:component-scan>
<mvc:annotation-driven/>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources location="/" mapping="/*.html"></mvc:resources>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 转换时间的bean方法 -->
<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"/>
</bean>
</set>
</property>
</bean>
</beans>
Formatter
Formatter就像Converter一样,也是将一种类型转换成另一种类型。但是,Formatter的源类型必须是一个·String,而Converter则适用于任意的源类型。Formatter更适合Web层,而Converter则可以用在任意层中。为了转换Spring MVC应用程序表单的用户输入,始终应该选择Formatter,而不适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 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) {
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(Exception e){
throw new IllegalArgumentException("invalid date format. please use this pattern\"" + datePattern + "\"");
}
}
}
xml配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="controller"></context:component-scan>
<context:component-scan base-package="formatter"></context:component-scan>
<mvc:annotation-driven conversion-service="conversionService"/>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources location="/" mapping="/*.html"></mvc:resources>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 转换时间的bean方法 -->
<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"/>
</bean>
</set>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/resource/message"></property>
</bean>
</beans>
选择Converter,还是Formatter
Converter是一般工具,可以将一种类型转换成另一种类型。例如,将String转换成Date,或者将Long转换成Date。Converter既可以用在Web层,也可以用在其他层中。
Formmtter只能将String转换成另一种Java类型。例如,将String转换成Date,但它不能将Long转换成Date。因此,Formatter更适合Web层。为此,在SpringMVC应用程序中,选择Formmatter比选择Converter和适合。