springmvc--数据转换器

来源:互联网 发布:网络传播负面消息方法 编辑:程序博客网 时间:2024/06/14 05:34

时间
@DateFormate

转换器

public class DateConverter implements Converter<String,Date> {    public Date convert(String source) {        String pattern = source.length()==10 ? "yyyy-MM-dd" : "yyyy-MM-dd HH:mm:ss";        SimpleDateFormat format = new SimpleDateFormat(pattern);        try {            return format.parse(source);        } catch (ParseException e) {            e.printStackTrace();        }        return null;    }}

注册转换器
在SpringMvc 的配置文件中注册 Converter

<mvc:annotation-driven conversion-service="customConversionService">    </mvc:annotation-driven>    <bean id="customConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <property name="converters">            <set>                <bean class="com.Util.DateConverter"/>                <bean class="com.Util.TimeSteamp"/>            </set>        </property>    </bean>
原创粉丝点击