Spring可以完成表达数据到对象属性的转换,其实默认提供了很多类型转换器。
详细内容可以参考http://www.cnblogs.com/wewill/p/5676920.html
本文主要讲解converter和formatter的使用。是类似helloworld的程序。
一、spring 中有许多方式实现自动的格式转换,都需要注册
<mvc:annotation-driven conversion-service="具体实现" />
实现:
(1)conversionService 可以转换任意类型为指定类型
(2)formaterService 只能转换String到指定类型,适合web工程
(3)formatterRegistrars 另一种注册formatter的形式,将formatter在registaras直接进行注册。
代码:
配置文件:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"
>
<!--<mvc:annotation-driven conversion-service="conversionService" />-->
<!--<mvc:annotation-driven conversion-service="formaterService" /> -->
<mvc:annotation-driven conversion-service="formatterRegistrars" />
<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"/>
</bean>
</list>
</property>
</bean>
<bean id = "formaterService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class = "DateFormMatter.DateFormatter">
<constructor-arg type="java.lang.String" value="MMddyyyy"/>
</bean>
</set>
</property>
</bean>
<bean id ="formatterRegistrars" class ="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatterRegistrars">
<set>
<bean class="DateFormMatter.MyFormatterRegistar">
<constructor-arg type="java.lang.String" value="MM-dd-yyyy"/>
</bean>
</set>
</property>
</bean>
</beans>
converters实现:
package 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;
@Override
public Date convert( String s){
SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
dateFormat.setLenient(false);
try {
return dateFormat.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new IllegalArgumentException("invalid date format ,Please use this pattern\""
+datePattern +"\"");
}
}
public StringToDateConverter(String datePattern)
{
this.datePattern = datePattern;
System.out.println("instantiating .... converter with pattern:*"+datePattern);;
}
}
formatter实现
package DateFormMatter;
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) {
super();
this.datePattern = datePattern;
dateFormat = new SimpleDateFormat(this.datePattern);
}
@Override
public String print(Date date, Locale locale) {
return dateFormat.format(date);
}
@Override
public Date parse(String s, Locale locale) throws ParseException {
return dateFormat.parse(s);
}
}
Registrars 定义
package DateFormMatter;
import org.springframework.format.FormatterRegistrar;
import org.springframework.format.FormatterRegistry;
public class MyFormatterRegistar implements FormatterRegistrar {
private String datePattern;
public MyFormatterRegistar(String datePattern)
{
this.datePattern = datePattern;
}
@Override
public void registerFormatters(FormatterRegistry register) {
// TODO Auto-generated method stub
register.addFormatter(new DateFormatter(datePattern));
}
}