天天看點

Spring MVC資料綁定大全 .

剛開始用spring mvc 做web開發時,經常會不知道如何合适綁定頁面資料.用慣struts2的朋友更認為spring mvc 綁定資料不如struts2友善(本人最開始也是這麼認為),經過一段時間的應用和測試,總結出以下幾種情況,希望對剛接觸spring mvc 的朋友有所幫助.

<a href="http://download.csdn.net/source/2738877">示例程式下載下傳</a>

檢視spring源碼可以看出spring支援轉換的資料類型已經很多了.

org.springframework.beans.PropertyEditorRegistrySupport.java

/** 

     * Actually register the default editors for this registry instance. 

     */  

    private void doRegisterDefaultEditors() {  

        this.defaultEditors = new HashMap&lt;Class, PropertyEditor&gt;(64);  

        // Simple editors, without parameterization capabilities.   

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

        this.defaultEditors.put(Charset.class, new CharsetEditor());  

        this.defaultEditors.put(Class.class, new ClassEditor());  

        this.defaultEditors.put(Class[].class, new ClassArrayEditor());  

        this.defaultEditors.put(Currency.class, new CurrencyEditor());  

        this.defaultEditors.put(File.class, new FileEditor());  

        this.defaultEditors.put(InputStream.class, new InputStreamEditor());  

        this.defaultEditors.put(Locale.class, new LocaleEditor());  

        this.defaultEditors.put(Pattern.class, new PatternEditor());  

        this.defaultEditors.put(Properties.class, new PropertiesEditor());  

        this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());  

        this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());  

        this.defaultEditors.put(URI.class, new URIEditor());  

        this.defaultEditors.put(URL.class, new URLEditor());  

        // Default instances of collection editors.   

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

        this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));  

        this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));  

        this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));  

        this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));  

        this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));  

        // Default editors for primitive arrays.   

        this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());  

        this.defaultEditors.put(char[].class, new CharArrayPropertyEditor());  

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

        this.defaultEditors.put(char.class, new CharacterEditor(false));  

        this.defaultEditors.put(Character.class, new CharacterEditor(true));  

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

        this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));  

        this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true));  

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

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

        this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));  

        this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));  

        this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));  

        this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));  

        this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));  

        this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));  

        this.defau

本文轉自茄子_2008部落格園部落格,原文連結:http://www.cnblogs.com/xd502djj/p/3245485.html,如需轉載請自行聯系原作者。