@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>
相关推荐
Spring MVC还支持其他高级特性,如拦截器(Interceptor)用于处理请求前后的通用逻辑,数据绑定和验证(Data Binding & Validation)简化了参数处理,以及多视图支持(Multiple Views)允许一个Controller方法返回多...
这个名为"springmvc.zip"的压缩包很可能是包含了一份关于如何使用Spring MVC框架进行开发的学习笔记或教程资料。接下来,我们将深入探讨Spring MVC的核心概念、主要功能以及一些关键的实践技巧。 1. **核心概念** ...
11. **数据绑定(Data Binding)**:Spring MVC自动将请求参数绑定到Controller方法的参数上,简化了数据处理过程。 12. **异常处理(Exception Handling)**:Spring MVC提供了一套优雅的异常处理机制,可以通过@...
在Spring配置文件中,你需要启用Multipart resolver,例如使用CommonsMultipartResolver,这样Spring才能处理multipart/form-data类型的请求。 ```xml <bean id="multipartResolver" class="org.springframework...
在Java Web开发中,SpringMVC是一个非常流行的MVC(Model-View-Controller)框架,它为构建可扩展且易于维护的Web应用程序提供了强大...通过阅读`SpringMVC_9_Data`文件,你可以深入学习这些特性的具体实现和示例代码。