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脚本 ...
随机推荐
- js全局变量
在做东钿微信公众号 ,首页有房产评估和产调,有个checkbox ,点击则选中使用积分,取消选中则不使用积分,html结构和css样式都一样,唯一不一样的就是数据不一样,于是我就分开来写,没有写同一个 ...
- 同时使用Binding&StringFormat 显示Text【项目】
Case ID (?unit) 红色的字根据一个后台boolean来做trigger,可以是Case or Open 蓝色的字binding到后台的一个string属性来切换任意的Unit单位 这样一 ...
- C# 递归算法与冒泡
C# 递归算法求 1,1,2,3,5,8,13···static void Main(string[] args){ int[] cSum = new int[10];for (int i = 0; ...
- PL SQL笔记(三)
loop then .. exit; end if; end loop; select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual; sel ...
- 【绝密外泄】风哥Oracle数据库DBA高级工程师培训视频教程与内部资料v0.1
[绝密外泄]风哥Oracle数据库DBA高级工程师培训视频教程与内部资料v0.1 由于是[绝密外泄]资料,防止被查,需要的小伙伴赶紧下载附件中的课件文档.由于视频太大了,已放在百度网盘了,已经在附中说 ...
- CentOS7安装telnet服务
CentOS7.0 telnet-server 启动的问题.解决方法: ①.先检查CentOS7.0是否已经安装以下两个安装包:telnet-server.xinetd.命令如下: rpm ...
- js正则表达式之解析——URL的正则表达式
首先,此片文章并不是直接告诉你,url的正则表达式是什么,以及怎么使用这个正则表达式去解析一个URL地址,相信这种问题在网络上已经能找到很多.本文的宗旨在于教你如何理解URL的正则表达式,以达到理解正 ...
- Microsoft Script Editor
目前,常用的浏览器IE.Chrome.Firefox都有相应的脚本调试功能.作为我们.NET 阵营,学会如何在IE中调试JS就足够了,在掌握了IE中的调试方法以后,Chrome和Firefox中的调试 ...
- 学习马士兵的struts2/hibernate/spring中遇到的问题及其解决方法
STRUTS2 1. 写好最简单的hello world项目后,无法通过浏览器访问到hello.jsp页面,提示没有资源. 学习structs2的时间,已经更新到了2.3.16了,structs中的很 ...
- 12个非常不错的免费HTML后台管理模板
下面介绍的这些免费后端管理HTML模板,都非常不错.建议您收藏. 1. Charisma Admin Template (示例) Charisma是一个响应式管理模板,基于Twitter Boots ...