自定义转换器实现Converter接口
//String是原始类型,Date是要转换的目标类型 public class CustomDateConverter implements
Converter<String, Date> { @Override public Date convert(String source) { try {
return new SimpleDateFormat("yyyy-MM-dd").parse(source); } catch (Exception e)
{ e.printStackTrace(); } return null; } }
配置转换器

配置方式一:针对不使用mvc驱动的
<!--注解适配器 --> <bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property> </bean>
<!-- 自定义webBinder --> <bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" /> </bean> <!--
conversionService --> <bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 --> <property name="converters"> <list> <bean
class="cn.itcast.ssm.controller.converter.CustomDateConverter"/> </list>
</property> </bean>
配置方式二:针对使用mvc驱动的
<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven> <!-- conversionService --> <bean
id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 --> <property name="converters"> <list> <bean
class="cn.itcast.ssm.controller.converter.CustomDateConverter"/> </list>
</property> </bean>
 

技术
今日推荐
PPT
阅读数 106
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信