spring 3.2 自定义参数绑定--日期格式转换器

来源:互联网 发布:广电总局网络主播新规 编辑:程序博客网 时间:2024/06/05 22:14

springmvc配置文件

<!-- 代替org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping和org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven><!-- 自定义参数绑定 --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">    <property name="converters">        <list>        <!-- 日期类型转换 -->            <bean class="converter.CustomDateConverter"></bean>        </list>    </property></bean>

org.springframework.format.support.FormattingConversionServiceFactoryBean

spring提供的converter的对外接口,可以注入自己写的converter转换器


需要向处理器适配器中注入自定义的参数绑定组件,本例接口在注解驱动

conversion-service="conversionService"


自定义的日期转换器

package converter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.core.convert.converter.Converter;public class CustomDateConverter implements Converter<String, Date> {    @Override    public Date convert(String source) {        //实现将日期串转换成日期类型,格式是yyyy-MM-dd HH:mm:ss        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        try {                        return simpleDateFormat.parse(source);        } catch (ParseException e) {            e.printStackTrace();        }        return null;    }}

0 0
原创粉丝点击