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>  
相关文章
相关标签/搜索