springMVC对简单对象、Set、List、Map的数据绑定和常见问题(一)


1、相关的类:

查看spring源码可以看出spring支持转换的数据类型:
org.springframework.beans.PropertyEditorRegistrySupport:

 

  1.   

  2. private void createDefaultEditors() {  

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

  4.   

  5.     // Simple editors, without parameterization capabilities.  

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

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

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

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

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

  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. }  

 

2、基本数据类型绑定:

 

  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或"",所以,在开发过程中,对可能为空的数据,最好将参数数据类型定义成包装类型。

 

3、包装类型

 

  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。

 

4、自定义对象类型

 

  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值一一对应即可。

 

5、自定义复合对象类型

 

  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。


转载于:https://my.oschina.net/u/2458458/blog/646874

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值