比如在页面我向springmvc中传入一个对象 对象中 有很多属性 但是我需要把日期类型属性通过我自定义的类型转换器进行转换
,它是怎么确定自定转换器类中接收的参数是对象中的日期属性。
public class DateConverter implements Converter {
@Override
public Date convert(String source) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
returnsimpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
returnnull;
}
}
<!-- 指定自定义转换器的全路径名称 -->
<mvc:annotation-driven conversion-service="conversionService" />
<!-- 自定义参数转换器 -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.bsw.mybatis.controller.converter.CustomDateConverter"></bean>
</list>
</property>
</bean
首先controller会自动匹配你jsp页面传过来的值是否和你参数定义的pojo中属性值名称相同,如果包含时间类型则需要在你的controller中自定义一个
日期转换器,当http请求过来时适配器去执行controller会默认调用自定义日期转换器,把http请求过来的日期数据转换成自定义中的日期格式。
// 自定义属性编辑器(早期)
@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"),true));
}