SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用
使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下:
方法一 . 在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。
/* 以下资料来自网络 */
用@InitBinder注解的控制器方法,允许你直接在你的控制器类中配置 Web 数据绑定。@InitBinder标记初始化WebDataBinder的方法,WebDataBinder被用于填充被注解的处理方法的命令和表单对象参数。
这些初始化绑定器(Init-binder)方法支持@RequestMapping方法支持的所有参数,处理命令/表单对象以及相关的校验结果对象。初始化绑定器方法必须不带返回值,所以它们通常被声明为 void 的。典型的参数包括WebDataBinder和WebRequest或 者java.util.Locale,允许用代码方式注册特定上下文的编辑器(context-specific editors)。
/* 以上资料来自网络 */
WebDataBinder是用来绑定请求参数到指定的属性编辑器.由于前台传到controller里的值是String类型的,当往Model里Set的数据是日期类型, Spring就会去找到对应的editor进行转换,将String类型转换成日期类型,然后再set进去.
我们要进行类型转换,需要在springMvc里配置配型转换器,springMvc提供了很多类型转化器,配置方法如下:
<!-- 日期转换 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<!--配置String类型的消息转换器-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=utf-8</value>
</list>
</property>
</bean>
<!--此处还可以定义其他类型的消息转换器 -->
</list>
</property>
</bean>
然后在需要进行类型转换的controller里创建初始化绑定器方法,用对应的编辑器将接受到的字符串转为日期,代码如下:
@Controller
public class MyFormController
{
@InitBinder
protected void initBinder(WebDataBinder binder)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
}
spring mvc在绑定数据之前,都会先注册这些编辑器,Spring提供了很多编辑器的实现类,像CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等,使用时候调用WebDataBinder的registerCustomEditor方法.
需要注意的是,此初始化绑定的方法,只在该Controller内生效.
这样,我们前台传递的字符串日期信息,在后台javaBean中使用Date就可以直接接收到了.
方法二: 使用@DateTimeFormat 注解方式,建议使用,此方式简单,且代码量少
首先,@DateTimeFormat 注解使用到了joda包中的东西, 我们需要引入joda相关jar包.如果不引入会报异常
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>
然后,我们要在javaBean 中要接收的Date类型数据上加上注解: 如下:
public class Person{
private String name;
private Integer age;
private Integer sex;
//在date类型上加入注解,同时指定接收到的日期格式样式
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;
}
最后,在SpringMVC配置XML文件中进行注解驱动的配置:
<!-- 日期转换 -->
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=utf-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
这样就完成了,后续只要的新增的javaBean中给日期类型加上@DateTimeFormat注解即可,任意contorller中使用javaBean接受数据时,传递的String类型数据会自动转换成对应日期类型
需要注意的是 接收字符串的日期格式需要与@DateTimeFormat注解后定义的格式一致.
SpringMVC 处理Date类型数据@InitBinder @DateTimeFormat 注解 的使用的更多相关文章
- 【Spring】SpringMVC中浅析Date类型数据的传递
在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...
- springMVC返回json数据时date类型数据被转成long类型
在项目的过程中肯定会遇到ajax请求,但是再用的过程中会发现,在数据库中好好的时间类型数据:2017-05-04 17:52:24 在转json的时候,得到的就不是时间格式了 而是145245121这 ...
- DATE类型数据在MySql中减一天的问题
最近在开发一个教务管理系统,数据库中有教师表(Teacher).学生表(Student)等,其中属性:出生日期(Birthday)为DATE类型. 在执行更新教师操作时,发现未改动教师的出生日期但更新 ...
- SpringMVC处理Date类型的成员变量方法
原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...
- Android向Rest服务Post数据遇到的Date类型数据问题
今天在Android端向Rest服务Post数据时,总是不成功,查了很多资料,才知道Rest端将json串反序列化时,需要的时间格式必须是UTC类型,及Date(12345678+0800)格式. A ...
- Spring的controller接受Date类型数据,接受枚举类型数据
1. Controller接收Date类型的数据 核心使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 来将传递过来的时间字符串 ...
- struts2 谷歌浏览器保存date类型数据时报错
一同事发现一个bug,在chrome上保存一个表单时,后台会报错,而在firefox上则可以正常保存. 奇怪的地方在于,后端的程序是同一个,而在浏览器上查看请求header时,两个浏览器对应的字段内容 ...
- 在oracle中存入date类型数据遇到的问题及其解决方法(利用java.sql.date和Timestamp)
转自:https://blog.csdn.net/ShadowerWArden/article/details/80652377 1. 使用JDBC操作Oracle数据库时,使用java.sql.Da ...
- debezium监听数据库变化Date类型数据的还原
debezium是一个开源的分布式CDC系统,支持对接各种数据源,将数据源中已持久化的数据变更捕获后写入消息队列. 当数据源是mysql时,debezium通过BINLOG实时捕获已提交事务数据. 在 ...
随机推荐
- php 获取最近一周,一个月,一年
<?php date_default_timezone_set('PRC'); /** * 获取最近一周,一个月,一年 * */ function getLatelyTime($type = ' ...
- 在见证了1000多家公司的兴衰灭亡之后,YC创始合伙人总结了创业公司的6个不死法则(转)
今天,我想先说一下个人消息.在 YC 工作了 11 年之后,我明年想去休假.我希望把精力放在一些项目上,说实话,我有点累了. YC 是这世界上我最喜欢的事情之一,但它也很费精力. 11 年不间断的耗费 ...
- 细数php里的那些“坑”
Part 1 Grammer 尽管PHP的语法已经很松散,写起来很“爽”.但是对于学过 Java 的“完全面向对象程序员“来说,PHP程序设计语言里,还是有一些的坑的.下面请让我来盘点一下. Pars ...
- Wxpython零基础制作计算器
本文关于Wxpython零基础利用python3.6在pycharm下制作计算器,文章末尾有免费源代码供下载 以后同步更新到博客园和这个网站,www.empirefree.top, 这个网站备案号没有 ...
- C语言动态链表数据结构
链表的操作增删改查 typedef int DATA; struct SNode { DATA data; SNode* pNext; }; SNode* g_head=NULL;//全局变量 //从 ...
- P2234 [HNOI2002]营业额统计(Splay树)题解
思路:Splay数查找前驱后继 代码: #include<iostream> #include<cstdio> #include<cstring> #include ...
- oracle单行函数 之 通用函数
NVL()函数,处理null. Decode()函数,:多数值判断 Decode(数值 \ 列,判断值1,显示值1,判断值2,显示值2)若是判断值不包含的,则显示为空 Decode()函数非常类似程序 ...
- Summary on Visual Tracking: Paper List, Benchmarks and Top Groups
Summary on Visual Tracking: Paper List, Benchmarks and Top Groups 2018-07-26 10:32:15 This blog is c ...
- No mapping found for HTTP request with URI [/Portal/download] in DispatcherServlet with name 'springmvc'
本文为博主原创,未经允许不得转载: 遇到这个异常,总结一下这个问题发生的原因: 这个原因是在springmvc中在DispatcherServlet分发请求时,解析不到相应的请求路径.后台要请求的路径 ...
- Python: find the smallest and largest value
题目要求: Write a program that repeatedly prompts a user for integer numbers until the user enters 'done ...