在数据绑定上,SpringMVC提供了到各种基本类型的转换,由前端到后台时,SpringMVC将字符串参数自动转换为各种基本类型。而对于其他,则需要自己编写代码进行转换。本随笔以转换时间类型为例,使用三种方式进行实现(其实是两种):

一、使用Converter

  转换器必须实现Converter接口,该接口原型如下:

public interface Converter<S, T> {
T convert(S source);
}

  编写DateConverter,该转换器可以自定义转换格式,默认为年-月-日:

public class DateConverter implements Converter<String, Date> {

    private String pattern = "yyyy-MM-dd";

    public void setPattern(String pattern) {
this.pattern = pattern;
} @Override
public Date convert(String source) {
if (source == null || source.trim().isEmpty())
return null;
DateFormat format = new SimpleDateFormat(pattern);
Date date = null;
try {
date = format.parse(source.trim());
} catch (ParseException e) {
}
return date;
} }

  接下来是注册该转换器:

<mvc:annotation-driven conversion-service="conversionService" />

<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="cn.powerfully.demo.web.controller.DateConverter" />
</set>
</property>
</bean>

二、使用WebBindingInitializer与Editor实现局部转换

  在需要实现转换的Controller添加如下代码即可实现转换,该方法采用Editor进行转换,它能够将String类型转换为其他类型

@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

三、使用WebBindingInitializer与Editor实现全局转换

  编写WebBindingInitializer的实现类

public class BindingInitializer implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
PropertyEditor propertyEditor = new CustomDateEditor(dateFormat , true);
binder.registerCustomEditor(Date.class, propertyEditor );
}
}

  在springmvc配置文件中注册WebBindingInitializer

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="cn.powerfully.demo.web.controller.BindingInitializer" />
</property>
</bean>

  特别注意的是:该bean必须定义在<mvc:annotation-driven />之前,否则无法生效。当然了,也可以使用注解方式:

@ControllerAdvice
public class WebBinder {
@InitBinder
public void initBinder(WebDataBinder binder){
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd HH:mm"));
}
}

总结:

  使用Converter能够将任意类型转换成其他类型。而是用Editor,则只能将String类型(前端接受到的数据)转换为其他类型。

SpringMVC之类型转换的更多相关文章

  1. springmvc的类型转换

     一.springmvc的类型转换 (一)默认情况下,springmvc内置的类型转换器只能 将"yyyy/MM/dd"类型的字符串转换为Date类型的日期 情境一: 而现在我们无 ...

  2. spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二)

    spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二) >>>>>>>>>>>>>>&g ...

  3. spring参数类型异常输出,SpringMvc参数类型转换错误输出

    spring参数类型异常输出, SpringMvc参数类型转换错误输出 >>>>>>>>>>>>>>>> ...

  4. 转:SpringMVC之类型转换Converter(GenericConverter)

    转: http://blog.csdn.net/fsp88927/article/details/37692215 SpringMVC 之类型转换 Converter 1.1 目录 1.1 目录 1. ...

  5. SpringMVC 之类型转换Converter详解转载

    SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1     目录 1.1      目录 1.2     ...

  6. SpringMVC 之类型转换Converter 源代码分析

    SpringMVC 之类型转换Converter 源代码分析 最近研究SpringMVC的类型转换器,在以往我们需要 SpringMVC 为我们自动进行类型转换的时候都是用的PropertyEdito ...

  7. SpringMVC之类型转换Converter

    (转载:http://blog.csdn.net/renhui999/article/details/9837897) 1.1     目录 1.1      目录 1.2      前言 1.3   ...

  8. [转]SpringMVC日期类型转换问题三大处理方法归纳

    http://blog.csdn.net/chenleixing/article/details/45190371 前言 我们在SpringMVC开发中,可能遇到比较多的问题就是前台与后台实体类之间日 ...

  9. springmvc参数类型转换三种方式

    SpringMVC绑定参数之类型转换有三种方式:     1. 实体类中加日期格式化注解      @DateTimeFormat(pattern="yyyy-MM-dd hh:MM&quo ...

  10. SpringMVC日期类型转换问题三大处理方法归纳

    方法一:实体类中加日期格式化注解 @DateTimeFormat(pattern = "yyyy-MM-dd") private Date receiveAppTime; 方法二: ...

随机推荐

  1. redmine 开机自动运行

    想要redmine在centos上开机自动运行,于是就在 /etc/rc.local里面加上了一行脚本 #!/bin/shecho "start redmine:"/usr/loc ...

  2. 20155326 《Java程序设计》实验五网络编程与安全实验报告

    20155326 <Java程序设计>实验五网络编程与安全实验报告 实验内容 任务一 1.两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/67667 ...

  3. IntelliJ IDEA配置Tomcat(完整版教程)

    查找该问题的童鞋我相信IntelliJ IDEA,Tomcat的下载,JDK等其他的配置都应该完成了,那我直接进入正题了. 1.新建一个项目 2.由于这里我们仅仅为了展示如何成功部署Tomcat,以及 ...

  4. ASP.NET Web API 框架研究 Controller实例的销毁

    我们知道项目中创建的Controller,如ProductController都继承自ApiController抽象类,其又实现了接口IDisposable,所以,框架中自动调用Dispose方法来释 ...

  5. C# 函数式编程及Monads.net库

    函数式编程中,一切皆为函数,这个函数一般不是类级别的,其可以保存在变量中,可以当做参数或返回值,是函数级别的抽象和重用,将函数作为可重用的基本模块,就像面向对象中一切皆为对象,把所有事物抽象为类,面向 ...

  6. 林纳斯·托瓦兹和Linux行为准则:揭穿7个谬论

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 作者:史蒂芬·沃恩·尼古斯(Steven J.Vaughan-Nichols),从事Linux开源工作 时间:格林威治标准时间2018年9月25日— ...

  7. urllib2 的使用与介绍

    爬虫简介  什么是爬虫? 爬虫:就是抓取网页数据的程序. HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的 ...

  8. python 通过pytz模块进行时区的转换,获取指定时区的时间

    import pytz import time import datetime print(pytz.country_timezones('cn')) # 查询中国所拥有的时区 print(pytz. ...

  9. 容器监控:cadvisor+influxdb+grafana

    cAdvisor:Google开源的工具,用于监控Docker主机和容器系统资源,通过图形页面实时显示数据,但不存储:它通过宿主机/proc./sys./var/lib/docker等目录下文件获取宿 ...

  10. cnetos6上实现nfs共享

    利用空余时间,做个nfs共享实验,岂不美滋滋!!! 系统测试环境: 主机ip 系统     主机名(服务) 192.168.241.130    centos6.5    hadoop1(nfs-sr ...