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脚本 ...
随机推荐
- [hihoCoder]#1039 : 字符消除
Description 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1)如果s包含长度超过1的由相同字母组成的子串,那么这些 ...
- List操作之Select
本文是写给C#新手,老手就勿看了,讲的实际上就是LINQ,谢谢一楼的提醒. 很多时候,从一个关系表中挑出一个我们需要的元素列表采用SQL语句是再容易不过的了,其实C#的List中也可以采用类似的方法, ...
- mvvm架构使用解析
配置 android studio目前已经集成了dataBinding,只需在build.gradle中配置,如下: android { dataBinding { enabled = true; } ...
- ExtJs非Iframe框架加载页面实现
在用Ext开发App应用时,一般的框架都是左边为菜单栏,中间为tab页方式的显示区域.而tab页面大多采用的嵌入一个iframe来显示内容.但是采用iframe方式有一个很大的弊端就是每次在加载一个新 ...
- ASSER、VERIFY、TRACE详解
ASSERT()被测试它的参数,如果参数为零,则中断执行并打印一段说明消息.在Release版本的程序中它不起任何作用. ASSERT()使用的时候必须保证参数表达式中不能有函数调用,因此对于任何有函 ...
- Lockless Ring Buffer Design
https://www.kernel.org/doc/Documentation/trace/ring-buffer-design.txt Lockless Ring Buffer Design == ...
- thinkphp 模板显示display和assign的用法
this->assign('name',$value); //在 Action 类里面使用 assign 方法对模板变量赋值,无论何种变量类型都统一使用 assign 赋值 $this-> ...
- 使用Python脚本进行域名解析
因为在研究爬虫,所以也了解了下域名解析.要提高爬虫的效率,就需要提高域名解析的效率.我将爬虫记录下的域名作为待解析的域名来测试各域名解析方法的效率.我尝试以下四种方法:1. 单线程依次解析各域名,2. ...
- ASP.NET中上传并读取Excel文件数据
在CSDN中,经常有人问如何打开Excel数据库文件.本文通过一个简单的例子,实现读取Excel数据文件. 首先,创建一个Web应用程序项目,在Web页中添加一个DataGrid控件.一个文件控件和一 ...
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...