Spring MVC 學習筆記 十一 data binding

@ResponseBody

做用: 該註解用於將Controller的方法返回的對象,經過適當的HttpMessageConverter轉換爲指定格式後,寫入到Response對象的body數據區。html

使用時機:返回的數據不是html標籤的頁面,而是其餘某種格式的數據時(如json、xml等)使用;java

二:

Servlet中的輸入參數爲都是string類型,而spring mvc經過data bind機制將這些string 類型的輸入參數轉換爲相應的command object(根據view和controller之間傳輸數據的具體邏輯,也可稱爲model attributes, domain model objects)。在這個轉換過程當中,spring實際是先利用java.beans.PropertyEditor中的 setAdText方法來把string格式的輸入轉換爲bean屬性, 
亦可經過繼承java.beans.PropertyEditorSupport來實現自定義的PropertyEditors,具體實現方式可參考spring reference 3.0.5 第 5.4節中的 Registering additional custom PropertyEditors部分。 
自定義完畢propertyEditor後,有如下幾種方式來註冊自定義的customer propertyEditor. 
1:直接將自定義的propertyEditor放到須要處理的java bean相同的目錄下 
名稱和java Bean相同但後面帶Editor後綴。 
例如須要轉換的java bean 名爲User,則在相同的包中存在UserEditor類可實現customer propertyEditor的自動註冊。 
2:利用@InitBinder來註冊customer propertyEditor 
這個在以前的筆記中已經介紹過了,即在controller類中增長一個使用@InitBinder標註的方法,在其中註冊customer Editor web

Java代碼 spring

  1. @InitBinder  
  2. public void initBinder(WebDataBinder binder) {  
  3.     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  4.     dateFormat.setLenient(false);  
  5.     binder.registerCustomEditor(Date.class, new CustomDateEditor(  
  6.             dateFormat, false));  
  7. }  


3:繼承 WebBindingInitializer 接口來實現全局註冊 
使用@InitBinder只能對特定的controller類生效,爲註冊一個全局的customer Editor,能夠實現接口WebBindingInitializer 。 json

Java代碼 mvc

  1. public class CustomerBinding implements WebBindingInitializer {  
  2.     @Override  
  3.     public void initBinder(WebDataBinder binder, WebRequest request) {  
  4.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  5.         dateFormat.setLenient(false);  
  6.         binder.registerCustomEditor(Date.class, new CustomDateEditor(  
  7.                 dateFormat, false));  
  8.   
  9.     }  


並修改 servlet context xml配置文件 app

Xml代碼 dom

  1. <bean  
  2.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  3.         <property name="webBindingInitializer">  
  4.             <bean  
  5.                 class="net.zhepu.web.customerBinding.CustomerBinding" />  
  6.         </property>  
  7.     </bean>  

但這樣一來就沒法使用mvc:annotation-driven  了。 
使用conversion-service來註冊自定義的converter 
DataBinder實現了PropertyEditorRegistry, TypeConverter這兩個interface,而在spring mvc實際處理時,返回值都是return binder.convertIfNecessary(見HandlerMethodInvoker中的具體處理邏輯)。所以能夠使用customer conversionService來實現自定義的類型轉換。 ide

Xml代碼 spa

  1. <bean id="conversionService"  
  2. class="org.springframework.format.support.FormattingConversionServiceFactoryBean">  
  3.   
  4. <property name="converters">  
  5.     <list>  
  6.         <bean class="net.zhepu.web.customerBinding.CustomerConverter" />  
  7.     </list>  
  8. </property>  
  9.   
  10. lt;/bean>  

須要修改spring service context xml配置文件中的annotation-driven,增長屬性conversion-service指向新增的conversionService bean。 

Xml代碼 

  1. <mvc:annotation-driven validator="validator"  
  2.     conversion-service="conversionService" />  

實際自定義的converter以下。 

Java代碼 

  1. public class CustomerConverter implements Converter<String, Date> {  
  2. @Override  
  3. public Date convert(String source) {  
  4.     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  5.     dateFormat.setLenient(false);  
  6.     try {  
  7.         return dateFormat.parse(source);  
  8.     } catch (ParseException e) {  
  9.         // TODO Auto-generated catch block  
  10.         e.printStackTrace();  
  11.     }         
  12.     return null;  
  13. }  


對於requestBody或httpEntity中數據的類型轉換 
Spring MVC中對於requestBody中發送的數據轉換不是經過databind來實現,而是使用HttpMessageConverter來實現具體的類型轉換。 
例如,以前提到的json格式的輸入,在將json格式的輸入轉換爲具體的model的過程當中,spring mvc首先找出request header中的contenttype,再遍歷當前所註冊的全部的HttpMessageConverter子類, 根據子類中的canRead()方法來決定調用哪一個具體的子類來實現對requestBody中的數據的解析。若是當前所註冊的httpMessageConverter中都沒法解析對應contexttype類型,則拋出HttpMediaTypeNotSupportedException (http 415錯誤)。 
那麼須要如何註冊自定義的messageConverter呢,很不幸,在spring 3.0.5中若是使用annotation-driven的配置方式的話,沒法實現自定義的messageConverter的配置,必須老老實實的本身定義AnnotationMethodHandlerAdapter的bean定義,再設置其messageConverters以註冊自定義的messageConverter。 
在3.1版本中,將增長annotation-driven對自定義的messageConverter的支持 (SPR-7504),具體格式以下 

Xml代碼 

  1. <mvc:annotation-driven>  
  2.     <mvc:message-converters>  
  3.         <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>  
  4.         <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>  
  5.         <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>  
  6.     </mvc:message-converters>  
  7. </mvc:annotation-driven>  
相關文章
相關標籤/搜索