1、PropertyEditor:内置可扩展,在类中进行局部使用 webdatabinder。(不推荐,一般使用全局方案会比较多)
2、Formatter:内置可扩展,全局,或者使用new Formatter的方式进行局部使用,只能转换String到其他类型。
3、Converter:内置不可扩展,全局或局部,和Formatter类似,但Converter的源对象不仅仅是String,而可以自行进行定义。
一、PropertyEditor
value = "date1.do")
public String date1(Date date1){
return date1.toString();
}
("date1")
public void initDate1(WebDataBinder binder){
binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
(
二、Formatter
package com.imooc.common;
import org.springframework.format.Formatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MyDateFormatter implements Formatter<Date> {
public Date parse(String text, Locale locale) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(text);
}
public String print(Date object, Locale locale) {
return null;
}
}
<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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com" annotation-config="true"/>
<mvc:annotation-driven conversion-service="myDateFormatter"/>
<bean id ="myDateFormatter" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<set>
<bean class="com.imooc.common.MyDateFormatter"></bean>
</set>
</property>
</bean>
</beans>
value = "date2.do")
public String date2(Date date2){
return date2.toString();
}
(
三、Converter
package com.imooc.common;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDateConverter implements Converter<String,Date> {
public Date convert(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
<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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com" annotation-config="true"/>
<mvc:annotation-driven conversion-service="myDateConverter"/>
<bean id ="myDateConverter" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.imooc.common.MyDateConverter"></bean>
<bean class="org.springframework.core.convert.support.StringToBooleanConverter"></bean>
</set>
</property>
</bean>
</beans>
value = "date2.do")
public String date2(Date date2){
return date2.toString();
}
(