當前台傳數據給spring的control時,如果后台方法的參數或參數對象的對應屬性是基本類型或者string類型時,前台值自動的注入到后台方法參數或對象屬性中。如果方法參數或對象屬性不是基本類型或string,比如是date類型,則不能直接賦值。
解決方法有一下三種:
一、使用注解
1、在springmvc配置文件中開啟注解<mvc:annotation-driven />
2、在參數或對象屬性上添加注解
如:如果是date類型可以添加
1、在springmvc配置文件中開啟注解<mvc:annotation-driven />
2、WebDataBinder是用來綁定請求參數到指定的屬性編輯器.由於前台傳到controller里的值是String類型的,當往Model里Set這個值的時候,如果set的這個屬性是個對象,Spring就會去找到對應的editor進行轉換,然后再set進去。
代碼如下:在自己的Controller代碼中加入:
1 @InitBinder 2 protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) { 3 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 4 simpleDateFormat.setLenient(false); 5 CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat, true); 6 binder.registerCustomEditor(Date.class, dateEditor); 7 } spring mvc在綁定表單之前,都會先注冊這些編輯器,Spring自己提供了大量的實現類,諸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等許多,基本上夠用。
spring mvc在綁定表單之前,都會先注冊這些編輯器,Spring自己提供了大量的實現類,諸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等許多,基本上夠用。
使用時候調用WebDataBinder的registerCustomEditor方法registerCustomEditor源碼:
public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) { getPropertyEditorRegistry().registerCustomEditor(requiredType, propertyEditor); }
第一個參數requiredType是需要轉化的類型。
第二個參數PropertyEditor是屬性編輯器,它是個接口,以上提到的如CustomDateEditor等都是繼承了實現了這個接口的PropertyEditorSupport類。
我們也可以不使用他們自帶的這些編輯器類。
我們可以構造自己的:
import org.springframework.beans.propertyeditors.PropertiesEditor; public class DoubleEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if (text == null || text.equals("")) { text = "0"; } setValue(Double.parseDouble(text)); } @Override public String getAsText() { return getValue().toString(); } }
此種方法作用預為本control,但是可以針對整個項目,像實例一樣抽出公共BaseController,讓所有的controller繼承BaseController,即可
1 @Controller 2 public class BaseController { 3 @InitBinder 4 protected void initBinder(WebDataBinder binder) { 5 binder.registerCustomEditor(Date.class, new MyDateEditor()); 6 /* 7 從PropertyEditorSupport 實現的方法可以看出,這里的綁定是為了處理空和null的數據, 8 開發者可根據實際情況綁定 9 */ 10 binder.registerCustomEditor(Double.class, new DoubleEditor()); 11 binder.registerCustomEditor(Integer.class, new IntegerEditor()); 12 } 13 14 private class MyDateEditor extends PropertyEditorSupport { 15 @Override 16 public void setAsText(String text) throws IllegalArgumentException { 17 //通過兩次異常的處理可以,綁定兩次日期的格式 18 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 19 Date date = null; 20 try { 21 date = format.parse(text); 22 } catch (ParseException e) { 23 format = new SimpleDateFormat("yyyy-MM-dd"); 24 try { 25 date = format.parse(text); 26 } catch (ParseException e1) { 27 log.error("系統異常:"+e1); 28 } 29 } 30 setValue(date); 31 } 32 } 33 34 public class DoubleEditor extends PropertiesEditor { 35 @Override 36 public void setAsText(String text) throws IllegalArgumentException { 37 try { 38 if (text == null || text.equals("")) { 39 text = "0"; 40 } 41 } catch (Exception e) { 42 log.error("系統異常:"+e); 43 } 44 45 setValue(Double.parseDouble(text)); 46 } 47 48 @Override 49 public String getAsText() { 50 return getValue().toString(); 51 } 52 } 53 54 public class IntegerEditor extends PropertiesEditor { 55 @Override 56 public void setAsText(String text) throws IllegalArgumentException { 57 try { 58 if (text == null || text.equals("")) { 59 text = "0"; 60 } 61 } catch (Exception e) { 62 log.error("系統異常:"+e); 63 } 64 65 setValue(Integer.parseInt(text)); 66 } 67 68 @Override 69 public String getAsText() { 70 return getValue().toString(); 71 } 72 } 73 }
三、定義的類型轉換類
1、自定義類型轉換規則
SpringMvc提供了Converter接口,它支持從一個Object轉換為另一個Object
1 public class DateConvert implements Converter<String, Date> { 2 3 @Override 4 public Date convert(String stringDate) { 5 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 6 try { 7 return simpleDateFormat.parse(stringDate); 8 } catch (ParseException e) { 9 e.printStackTrace(); 10 } 11 return null; 12 } 13 14 }
2、注冊自定義的類型轉換類
1 <!-- 第一步: 創建自定義日期轉換規則 --> 2 <bean id="dateConvert" class="zpark.convert.DateConvert"/> 3 <!-- 第二步: 創建convertion-Service ,並注入dateConvert--> 4 <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 5 <property name="converters"> 6 <set> 7 <ref bean="dateConvert"/> 8 </set> 9 </property> 10 </bean> 11 <!-- 第三步:注冊處理器映射器/處理器適配器 ,添加conversion-service屬性--> 12 <mvc:annotation-driven conversion-service="conversionService"/>