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
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
3:继承 WebBindingInitializer 接口来实现全局注册
使用@InitBinder只能对特定的controller类生效,为注册一个全局的customer Editor,可以实现接口WebBindingInitializer 。
public class CustomerBinding implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
并修改 servlet context xml配置文件
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="net.zhepu.web.customerBinding.CustomerBinding" />
</property>
</bean>
但这样一来就无法使用mvc:annotation-driven 了。
使用conversion-service来注册自定义的converter
DataBinder实现了PropertyEditorRegistry, TypeConverter这两个interface,而在spring mvc实际处理时,返回值都是return binder.convertIfNecessary(见HandlerMethodInvoker中的具体处理逻辑)。因此可以使用customer conversionService来实现自定义的类型转换。
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="net.zhepu.web.customerBinding.CustomerConverter" />
</list>
</property>
</bean>
需要修改spring service context xml配置文件中的annotation-driven,增加属性conversion-service指向新增的conversionService bean。
<mvc:annotation-driven validator="validator"
conversion-service="conversionService" />
实际自定义的converter如下。
public class CustomerConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
try {
return dateFormat.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
对于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),具体格式如下
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
分享到:
相关推荐
The Spring Framework Chapter 2: Model 2 and the MVC Pattern Chapter 3: Introduction to Spring MVC Chapter 4: Annotation-Based Controllers Chapter 5: Data Binding and the Form Tag Library Chapter 6: ...
Data Binding with Windows Forms 2.0: Programming Smart Client Data Applications with .NET By Brian Noyes ............................................... Publisher: Addison Wesley ...
Android Data Binding结合RecyclerView的使用
3.0.5.RELEASE.jar,org.springframework.asm-3.0.5.RELEASE.jar,org.springframework.aspects-3.0.5.RELEASE.jar,org.springframework.beans-3.0.5.RELEASE.jar,org.springframework.binding-2.3.0.RELEASE.jar...
CVE-2022-22963 复现Demo,A Spring MVC or Spring WebFlux application running on JDK 9+ may be vulnerable to remote code execution (RCE) via data binding. The specific exploit requires the application ...
WPF Binding 学习笔记
This book is all about the what and the why of binding to data sources in a Windows Forms application built using Visual Studio 2005. The book goes into great detail in explaining the rationale behind...
Android Data Binding实战-入门篇学习笔记,包含实例代码。 基于 Android Studio平台。
These tutorials describe how to map your classes to your tables manually (rather than with an automated tool like SqlMetal) so that you can have support for M:M relationships and data binding against ...
Core technologies: dependency injection, events, resources, i18n, validation, data binding, type conversion, SpEL, AOP. Testing: mock objects, TestContext framework, Spring MVC Test, WebTestClient. ...
Brian Noyes is a software architect, trainer, writer, and speaker with IDesign (www.idesign.net), a premier .NET architecture and design consulting and training company. He has been developing ...
豆瓣电影搜索 利用Data Binding在RecycleView展示电影列表
Data Binding with Windows Forms 2.0 Programming Smart Client Data Applications with .NET
In this chapter, we examine data binding, the ability to associate non-GUI objects that hold and maintain data with the controls that present and receive that data
Spring MVC(Model-View-Controller)是一个基于Java的MVC...7. 数据绑定(Data Binding) 8. 校验器(Validator) 这些资源描述了Spring MVC框架中不同组件的职责和作用,协同工作以构建可靠和可扩展的Web应用程序。
AndroidDataBindingExample, Android Data Binding 代码实战
In this chapter, we examine data binding, the ability to associate non-GUI objects that hold and maintain data with the controls that present and receive that data
主要介绍了Android Data Binding 在 library module 中遇到错误及解决办法的相关资料,需要的朋友可以参考下
Android Data Binding Library 官方文档中文翻译,出自https://blog.csdn.net/jjwwmlp456/article/details/54915981的博客,被我制作成离线版pdf以供保存, Google官方文档地址:...