在数据绑定上,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. 6609 - Minimal Subarray Length

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. python常用算法题

    一: #计算平均数,中位数和众数 #coding:utf-8 #计算平均数,中位数和众数 import random lst = [random.randint(0,10) for i in rang ...

  3. 冲刺博客NO.3

    今天做了什么:参考网上的一些登录界面,发现了Android studio自带loginActivity.做了基础登录界面 不停地上网搜,各种不会.  在短信验证功能上通过在Mob.com的集成文档和官 ...

  4. Delphi程序带参数运行

    程序1 program E1; uses Forms,Dialogs,SysUtils, EndM1 in 'EndM1.pas' {Form2}; {$R *.res} begin Applicat ...

  5. WPF点滴(3) 行为-Behavior

    为了定制个性化的用户界面,我们通常会借助于WPF强大的样式(style),修改控件属性,重写控件模板(template),样式帮助我们构建一致的个性化控件.通过样式可以调整界面的显示效果,这只是界面构 ...

  6. 【转】基于Token的WEB后台认证机制

    原谅地址:http://www.cnblogs.com/xiekeli/p/5607107.html 几种常用的认证机制 HTTP Basic Auth HTTP Basic Auth简单点说明就是每 ...

  7. Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)

    题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...

  8. Android逆向进阶—— 脱壳的奥义(基ART模式下的dump)

    本文作者:i春秋作家HAI_ZHU 0×00 前言 市面上的资料大多都是基于Dalvik模式的dump,所以这此准备搞一个ART模式下的dump.HAI_的使用手册(各种好东西) Dalvik模式是A ...

  9. C#6.0语言规范(十三) 接口

    接口定义合同.实现接口的类或结构必须遵守其合同.接口可以从多个基接口继承,并且类或结构可以实现多个接口. 接口可以包含方法,属性,事件和索引器.接口本身不为它定义的成员提供实现.接口仅指定必须由实现接 ...

  10. 【ElasticSearch】:索引Index、文档Document、字段Field

    因为从ElasticSearch6.X开始,官方准备废弃Type了.对应数据库,对ElasticSearch的理解如下: ElasticSearch 索引Index 文档Document 字段Fiel ...