springMVC對簡單對象、Set、List、Map的數據綁定和常見問題(一)


一、相關的類:
java

查看spring源碼能夠看出spring支持轉換的數據類型:
org.springframework.beans.PropertyEditorRegistrySupport:spring

 

  1.   

  2. private void createDefaultEditors() {  app

  3.     this.defaultEditors = new HashMap(64);  jsp

  4.   

  5.     // Simple editors, without parameterization capabilities.  ide

  6.     // The JDK does not contain a default editor for any of these target types.  post

  7.     this.defaultEditors.put(Charset.classnew CharsetEditor());  this

  8.     this.defaultEditors.put(Class.classnew ClassEditor());  spa

  9.     this.defaultEditors.put(Class[].classnew ClassArrayEditor());  code

  10.     this.defaultEditors.put(Currency.classnew CurrencyEditor());  orm

  11.     this.defaultEditors.put(File.classnew FileEditor());  

  12.     this.defaultEditors.put(InputStream.classnew InputStreamEditor());  

  13.     this.defaultEditors.put(InputSource.classnew InputSourceEditor());  

  14.     this.defaultEditors.put(Locale.classnew LocaleEditor());  

  15.     this.defaultEditors.put(Pattern.classnew PatternEditor());  

  16.     this.defaultEditors.put(Properties.classnew PropertiesEditor());  

  17.     this.defaultEditors.put(Resource[].classnew ResourceArrayPropertyEditor());  

  18.     this.defaultEditors.put(TimeZone.classnew TimeZoneEditor());  

  19.     this.defaultEditors.put(URI.classnew URIEditor());  

  20.     this.defaultEditors.put(URL.classnew URLEditor());  

  21.     this.defaultEditors.put(UUID.classnew UUIDEditor());  

  22.   

  23.     // Default instances of collection editors.  

  24.     // Can be overridden by registering custom instances of those as custom editors.  

  25.     this.defaultEditors.put(Collection.classnew CustomCollectionEditor(Collection.class));  

  26.     this.defaultEditors.put(Set.classnew CustomCollectionEditor(Set.class));  

  27.     this.defaultEditors.put(SortedSet.classnew CustomCollectionEditor(SortedSet.class));  

  28.     this.defaultEditors.put(List.classnew CustomCollectionEditor(List.class));  

  29.     this.defaultEditors.put(SortedMap.classnew CustomMapEditor(SortedMap.class));  

  30.   

  31.     // Default editors for primitive arrays.  

  32.     this.defaultEditors.put(byte[].classnew ByteArrayPropertyEditor());  

  33.     this.defaultEditors.put(char[].classnew CharArrayPropertyEditor());  

  34.   

  35.     // The JDK does not contain a default editor for char!  

  36.     this.defaultEditors.put(char.classnew CharacterEditor(false));  

  37.     this.defaultEditors.put(Character.classnew CharacterEditor(true));  

  38.   

  39.     // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.  

  40.     this.defaultEditors.put(boolean.classnew CustomBooleanEditor(false));  

  41.     this.defaultEditors.put(Boolean.classnew CustomBooleanEditor(true));  

  42.   

  43.     // The JDK does not contain default editors for number wrapper types!  

  44.     // Override JDK primitive number editors with our own CustomNumberEditor.  

  45.     this.defaultEditors.put(byte.classnew CustomNumberEditor(Byte.classfalse));  

  46.     this.defaultEditors.put(Byte.classnew CustomNumberEditor(Byte.classtrue));  

  47.     this.defaultEditors.put(short.classnew CustomNumberEditor(Short.classfalse));  

  48.     this.defaultEditors.put(Short.classnew CustomNumberEditor(Short.classtrue));  

  49.     this.defaultEditors.put(int.classnew CustomNumberEditor(Integer.classfalse));  

  50.     this.defaultEditors.put(Integer.classnew CustomNumberEditor(Integer.classtrue));  

  51.     this.defaultEditors.put(long.classnew CustomNumberEditor(Long.classfalse));  

  52.     this.defaultEditors.put(Long.classnew CustomNumberEditor(Long.classtrue));  

  53.     this.defaultEditors.put(float.classnew CustomNumberEditor(Float.classfalse));  

  54.     this.defaultEditors.put(Float.classnew CustomNumberEditor(Float.classtrue));  

  55.     this.defaultEditors.put(double.classnew CustomNumberEditor(Double.classfalse));  

  56.     this.defaultEditors.put(Double.classnew CustomNumberEditor(Double.classtrue));  

  57.     this.defaultEditors.put(BigDecimal.classnew CustomNumberEditor(BigDecimal.classtrue));  

  58.     this.defaultEditors.put(BigInteger.classnew CustomNumberEditor(BigInteger.classtrue));  

  59.   

  60.     // Only register config value editors if explicitly requested.  

  61.     if (this.configValueEditorsActive) {  

  62.         StringArrayPropertyEditor sae = new StringArrayPropertyEditor();  

  63.         this.defaultEditors.put(String[].class, sae);  

  64.         this.defaultEditors.put(short[].class, sae);  

  65.         this.defaultEditors.put(int[].class, sae);  

  66.         this.defaultEditors.put(long[].class, sae);  

  67.     }  

  68. }  

 

二、基本數據類型綁定:

 

  1. @RequestMapping("test.do")    

  2. public void test(int num) {    

  3.         

  4. }   

  5. "test.do" method="post">  

  6.    "num" value="10" type="text"/>  

  7.    ......  

  8.   


注意:表單中input的name值和Controller的參數變量名保持一致,就能完成基本數據類型的數據綁定,若是不一致能夠使用@RequestParam標註實現。值得一提的是,若是Controller方法參數中定義的是基本數據類型,可是從jsp提交過來的數據爲null或者""的話,會出現數據轉換的異常。也就是說,必須保證表單傳遞過來的數據不能爲null或"",因此,在開發過程當中,對可能爲空的數據,最好將參數數據類型定義成包裝類型。

 

三、包裝類型

 

  1. @RequestMapping("test.do")  

  2. public void test(Integer num) {  

  3.       

  4. }  


  1. "test.do" method="post">  

  2.    "num" value="10" type="text"/>  

  3.    ......  

  4.   


和基本數據類型基本同樣,不一樣之處在於,JSP表單傳遞過來的數據能夠爲null或"",以上面代碼爲例,若是jsp中num爲""或者表單中無num這個input,那麼,Controller方法參數中的num值則爲null。

 

四、自定義對象類型

 

  1. public class User {  

  2.   

  3.     private String firstName;  

  4.   

  5.     private String lastName;  

  6.   

  7.     ...  

  8.   

  9. }  

  10. @RequestMapping("test.do")  

  11. public void test(User user) {  

  12.       

  13. }  


  1. "test.do" method="post">  

  2.    "firstName" value="張" type="text"/>  

  3.    "lastName" value="三" type="text"/>  

  4.    ......  

  5.   


只需將對象的屬性名和input的name值一一對應便可。

 

五、自定義複合對象類型

 

  1. public class ContactInfo {  

  2.   

  3.     private String tel;  

  4.   

  5.     private String address;  

  6.   

  7.     。。。  

  8.   

  9. }  

  10.   

  11. public class User {  

  12.   

  13.     private String firstName;  

  14.   

  15.     private String lastName;  

  16.   

  17.     private ContactInfo contactInfo;  

  18.   

  19.     。。。  

  20.   

  21. }  


  1. @RequestMapping("test.do")  

  2. public void test(User user) {  

  3.     System.out.println(user.getFirstName());  

  4.     System.out.println(user.getLastName());  

  5.     System.out.println(user.getContactInfo().getTel());  

  6.     System.out.println(user.getContactInfo().getAddress());  

  7. }  


  1. <</span>form action="test.do" method="post">  

  2.    <</span>input name="firstName" value="張" /><</span>br>  

  3.    <</span>input name="lastName" value="三" /><</span>br>  

  4.    <</span>input name="contactInfo.tel" value="13809908909" /><</span>br>  

  5.    <</span>input name="contactInfo.address" value="北京海淀" /><</span>br>  

  6.    <</span>input type="submit" value="Save" />  

  7. </</span>form>  


User對象中有ContactInfo屬性,Controller中的代碼和第3點說的一致,可是,在jsp代碼中,須要使用「屬性名(對象類型的屬性).屬性名」來命名input的name。

相關文章
相關標籤/搜索