SpringMVC自定义类型转换器,String转Date
首先创建自定义转换器类,实现Converter接口
public class Convert implements Converter<String,Date> { public Date convert(String s) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("aaaaaaaaaa"); try { return formatter.parse(s); } catch (ParseException e) { e.printStackTrace(); } return null; } }
然后在配置文件中加入配置
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 自定义参数绑定 --> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="cn.lhl.converter.Convert"></bean> </set> </property> </bean>