springmvc(3)--数据类型转换
springmvc 配置 中conversionService可以配置类型转换,springmvc 参数绑定 中各种绑定方式和注解就是使用的这些转换器
一、先看下spring提供的内建类型转换器
- 第一组:标量转换器
- 第二组:集合、数组相关转换器
- 第三组:默认转换器,以上转换器不能转换时调用
- 在Controller范围,使用@InitBinder来注册customer propertyEditor
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));
}
注册"yyyy-MM-dd"日期格式的转换器
- 实现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));
}
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="springtry.web.util.MyWebBindingInitializer" />
</property>
</bean>
- 使用conversion-service来注册自定义的converter,同样是全局范围
public class MyCustomerConverter 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) {
e.printStackTrace();
}
return null;
}
}
<mvc:annotation-driven conversion-service="conversionService" /> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="springtry.web.util.MyCustomerConverter" />
</set>
</property>
</bean>
三、着重讲下@RequestBody使用的转换器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!--json转换器-->
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes"><!--response的Content-Typ-->
<list>
<value>text/html;charset=UTF-8</value><!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
默认的messageConverters包含了以下几种
ByteArrayHttpMessageConverter: 负责读取二进制格式的数据和写出二进制格式的数据;StringHttpMessageConverter: 负责读取字符串格式的数据和写出二进制格式的数据;- ResourceHttpMessageConverter:负责读取资源文件和写出资源文件数据;
- FormHttpMessageConverter: 负责读取form提交的数据(能读取的数据格式为 application/x-www-form-urlencoded,不能读取multipart/form-data格式数据);负责写入application/x-www-from-urlencoded和multipart/form-data格式的数据;
- MappingJackson2HttpMessageConverter: 负责读取和写入json格式的数据;
- SouceHttpMessageConverter: 负责读取和写入 xml 中javax.xml.transform.Source定义的数据;
- Jaxb2RootElementHttpMessageConverter: 负责读取和写入xml 标签格式的数据;
- AtomFeedHttpMessageConverter: 负责读取和写入Atom格式的数据;
- RssChannelHttpMessageConverter: 负责读取和写入RSS格式的数据;
springmvc(3)--数据类型转换的更多相关文章
- Springmvc 进行数据类型转换
SpringMVC进行一些常用的数据类型转换,这里以Date 数据类型的转换为例. SpringMVC表单中输入日期,一般都是以字符串的形式输入,如何将字符形式的日期转换为Date 类型的呢?这里只需 ...
- SpringMVC框架下数据的增删改查,数据类型转换,数据格式化,数据校验,错误输入的消息回显
在eclipse中javaEE环境下: 这儿并没有连接数据库,而是将数据存放在map集合中: 将各种架包导入lib下... web.xml文件配置为 <?xml version="1. ...
- SpringMVC(三)-- 视图和视图解析器、数据格式化标签、数据类型转换、SpringMVC处理JSON数据、文件上传
1.视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象 对于那些返回 String,View 或 ModeMap 等类型的处理方法,SpringMVC 也会在内部将 ...
- 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验
12.数据类型转换.格式化.校验 12.1.数据绑定流程 Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFacto ...
- SpringMVC 数据转换 & 数据格式化 & 数据校验
数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFactory 实例,以创建 DataBinder 实例对象 ...
- SpringMVC——数据转换 & 数据格式化 & 数据校验
一.数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标方 法的入参实例传递给 WebDataBinderFactory 实例,以创 建 DataBinder ...
- Spring(六)SpringMVC的数据响应
SpringMVC的请求和响应 SpringMVC的数据响应 01-SpringMVC的数据响应-数据响应方式(理解) 1) 页面跳转 直接返回字符串 通过ModelAndView对象返回 2) ...
- 第24章 Java 数据类型转换
每日一句 井底点灯深烛伊,共郎长行莫围棋. 每日一句 What we call "failure" is not falling down, but the staying dow ...
- springmvc的数据校验
springmvc的数据校验 在Web应用程序中,为了防止客户端传来的数据引发程序异常,常常需要对数据进行验证,输入验证分为客户端验证与服务器端验证. 客户端验证主要通过javaScript脚本 ...
随机推荐
- 乱侃c++
就在刚才我感觉c++真的好复杂,函数重载,多态,虚函数,虚函数表,模版,继承等一大坨东西好恶心,c++既然完全支持C语言,当然是把它的优缺点统统接下了,C语言中指针本身并不太难,是C语言的精华,当年刚 ...
- 编译ffmpeg(iOS)
一,x264库的编译 首先到http://www.videolan.org/developers/x264.html下载x264的库,然后解压,修改文件夹名称为x264 二,下载ffmpeg2 ...
- 【待补】java开发Web Service
Java中WebService实例 http://blog.csdn.net/kardelpeng/article/details/6321019 java 调用webservice的各种方法总结 h ...
- PL/SQL devloper 常用设置
1)代码自动完成 Tools->Preferences->User Interface->Key Configuration. 找到Tools/Code Assistant,修改为自 ...
- jQuery(一)delegate() 方法
定义和用法 delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数. 使用 delegate() 方法的事件处理程序适用于当前或未来 ...
- NAT类型与穿透 及 STUN TURN 协议
STUN : Simple Traversal of User Datagram Protocol [UDP] Through Network Address Translators [NATs] S ...
- VPW协议解析
http://www.dpfdoctor.net/content/?220.html SAE J1850 VPW协议也是OBD II标准中的一种,通常应用于GM车系中. VPW英文全称是Variabl ...
- php safe mode bypass all <转>
PHP safe mode bypass from 4.x to 5.x all. Functions: * mb_send_mail* curl_init* imap_open* mail* ion ...
- Mysql权限控制 - 允许用户远程连接
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...
- hdu 5273 Dylans loves sequence 逆序数简单递推
Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...