springMVC 绑定参数 类型转换

一、使用注解式控制器注册PropertyEditor(针对具体的controller类处理)

1、使用WebDataBinder进行控制器级别的注册PropertyEditor(控制器独享)

 

Java代码   收藏代码
  1. @InitBinder  
  2. // 此处的参数也可以是ServletRequestDataBinder类型  
  3. public void initBinder(WebDataBinder binder) throws Exception {  
  4.     // 注册自定义的属性编辑器  
  5.     // 1、日期  
  6.     DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  7.     //CustomDateEditor类是系统内部自带的类  
  8.     CustomDateEditor dateEditor = new CustomDateEditor(df, true);  
  9.     // 表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换  
  10.     binder.registerCustomEditor(Date.class, dateEditor);  
  11.     // 自定义的电话号码编辑器(和【4.16.1、数据类型转换】一样)  
  12.     binder.registerCustomEditor(PhoneNumberModel.classnew PhoneNumberEditor());  
  13. }  

 

 备注:转换对象必须要实现PropertyEditor接口,例如CustomDateEditor类

 

Java代码   收藏代码
  1. package org.springframework.beans.propertyeditors;  
  2.   
  3. import java.beans.PropertyEditorSupport;  
  4. import java.text.DateFormat;  
  5. import java.text.ParseException;  
  6. import java.util.Date;  
  7. import org.springframework.util.StringUtils;  
  8.   
  9. public class CustomDateEditor extends PropertyEditorSupport {  
  10.   
  11.     private final DateFormat dateFormat;  
  12.     private final boolean allowEmpty;  
  13.     private final int exactDateLength;  
  14.       
  15.     public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) {  
  16.         this.dateFormat = dateFormat;  
  17.         this.allowEmpty = allowEmpty;  
  18.         exactDateLength = -1;  
  19.     }  
  20.   
  21.     public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty, int exactDateLength) {  
  22.         this.dateFormat = dateFormat;  
  23.         this.allowEmpty = allowEmpty;  
  24.         this.exactDateLength = exactDateLength;  
  25.     }  
  26.   
  27.     public void setAsText(String text) throws IllegalArgumentException {  
  28.         if (allowEmpty && !StringUtils.hasText(text)) {  
  29.             setValue(null);  
  30.         } else {  
  31.             if (text != null && exactDateLength >= 0 && text.length() != exactDateLength)  
  32.                 throw new IllegalArgumentException((new StringBuilder("Could not parse date: it is not exactly")).append(exactDateLength).append("characters long").toString());  
  33.             try {  
  34.                 setValue(dateFormat.parse(text));  
  35.             } catch (ParseException ex) {  
  36.                 throw new IllegalArgumentException((new StringBuilder("Could not parse date: ")).append(ex.getMessage()).toString(), ex);  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41.     public String getAsText() {  
  42.         Date value = (Date) getValue();  
  43.         return value == null ? "" : dateFormat.format(value);  
  44.     }  
  45.   
  46. }  

 

2、使用xml配置实现类型转换(系统全局转换器)

(1、注册ConversionService实现和自定义的类型转换器 

Xml代码   收藏代码
  1. <!-- ①注册ConversionService -->  
  2. <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">  
  3.     <property name="converters">  
  4.        <list>  
  5.             <bean class="hb.base.convert.DateConverter">  
  6.                 <constructor-arg value="yyyy-MM-dd"/>  
  7.             </bean>  
  8.         </list>  
  9.     </property>  
  10.     <!-- 格式化显示的配置  
  11.     <property name="formatters">  
  12.         <list>  
  13.             <bean class="hb.base.convert.DateFormatter">  
  14.                 <constructor-arg value="yyyy-MM-dd"/>  
  15.             </bean>  
  16.         </list>  
  17.     </property>  
  18. -->  
  19. </bean>  

 

(2、使用 ConfigurableWebBindingInitializer 注册conversionService

Xml代码   收藏代码
  1. <!-- ②使用 ConfigurableWebBindingInitializer 注册conversionService -->  
  2. <bean id="webBindingInitializer"  
  3.     class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">  
  4.     <property name="conversionService" ref="conversionService" />  
  5.     <property name="validator" ref="validator" />  
  6. </bean>  

 

(3、注册ConfigurableWebBindingInitializer 到RequestMappingHandlerAdapter

Xml代码   收藏代码
  1. <!--Spring3.1开始的注解 HandlerAdapter -->  
  2. <bean  
  3.     class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
  4.     <!--线程安全的访问session -->  
  5.     <property name="synchronizeOnSession" value="true" />  
  6.     <property name="webBindingInitializer" ref="webBindingInitializer"/>  
  7. </bean>  

 

此时可能有人会问,如果我同时使用 PropertyEditor 和 ConversionService,执行顺序是什么呢?内部首先查找PropertyEditor 进行类型转换,如果没有找到相应的 PropertyEditor 再通过 ConversionService进行转换。 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值