使用SpringMVC的时候,需要将表单中的日期字符串转换成对应JavaBean的Date类型,而SpringMVC默认不支持这个格式的转换,解决方法有两种,如下:

方法一 . 在需要日期转换的Controller中使用SpringMVC的注解@initbinder和Spring自带的WebDateBinder类来操作。

/* 以下资料来自网络 */

@InitBinder注解的控制器方法,允许你直接在你的控制器类中配置 Web 数据绑定。@InitBinder标记初始化WebDataBinder的方法,WebDataBinder被用于填充被注解的处理方法的命令和表单对象参数。

     这些初始化绑定器(Init-binder)方法支持@RequestMapping方法支持的所有参数,处理命令/表单对象以及相关的校验结果对象。初始化绑定器方法必须不带返回值,所以它们通常被声明为 void 的。典型的参数包括WebDataBinderWebRequest或 者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 注解 的使用的更多相关文章

  1. 【Spring】SpringMVC中浅析Date类型数据的传递

    在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...

  2. springMVC返回json数据时date类型数据被转成long类型

    在项目的过程中肯定会遇到ajax请求,但是再用的过程中会发现,在数据库中好好的时间类型数据:2017-05-04 17:52:24 在转json的时候,得到的就不是时间格式了 而是145245121这 ...

  3. DATE类型数据在MySql中减一天的问题

    最近在开发一个教务管理系统,数据库中有教师表(Teacher).学生表(Student)等,其中属性:出生日期(Birthday)为DATE类型. 在执行更新教师操作时,发现未改动教师的出生日期但更新 ...

  4. SpringMVC处理Date类型的成员变量方法

    原文链接:http://www.tuicool.com/articles/aYfaqa 在使用 SpringMVC 的时候,我们可能需要将一个对象从 View 传递给 Controller .而当这个 ...

  5. Android向Rest服务Post数据遇到的Date类型数据问题

    今天在Android端向Rest服务Post数据时,总是不成功,查了很多资料,才知道Rest端将json串反序列化时,需要的时间格式必须是UTC类型,及Date(12345678+0800)格式. A ...

  6. Spring的controller接受Date类型数据,接受枚举类型数据

    1. Controller接收Date类型的数据 核心使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 来将传递过来的时间字符串 ...

  7. struts2 谷歌浏览器保存date类型数据时报错

    一同事发现一个bug,在chrome上保存一个表单时,后台会报错,而在firefox上则可以正常保存. 奇怪的地方在于,后端的程序是同一个,而在浏览器上查看请求header时,两个浏览器对应的字段内容 ...

  8. 在oracle中存入date类型数据遇到的问题及其解决方法(利用java.sql.date和Timestamp)

    转自:https://blog.csdn.net/ShadowerWArden/article/details/80652377 1. 使用JDBC操作Oracle数据库时,使用java.sql.Da ...

  9. debezium监听数据库变化Date类型数据的还原

    debezium是一个开源的分布式CDC系统,支持对接各种数据源,将数据源中已持久化的数据变更捕获后写入消息队列. 当数据源是mysql时,debezium通过BINLOG实时捕获已提交事务数据. 在 ...

随机推荐

  1. Golang指针基本介绍及使用案例

    一.指针的相关概念说明 变量:是基本类型,变量存的就是值,也叫值类型 地址:用于引用计算机的内存地址,可理解为内存地址的标签,通俗一点讲就是一间房在小区里的门牌号.如下图① 指针:指针变量存的是一个地 ...

  2. Python爬虫(二)——豆瓣图书决策树构建

    前文参考:  https://www.cnblogs.com/LexMoon/p/douban1.html Matplotlib绘制决策树代码: # coding=utf-8 import matpl ...

  3. 12: nginx原理及常用配置

    1.1 nginx基本介绍 1.nginx高并发原理( 多进程+epoll实现高并发 ) 1. Nginx 在启动后,会有一个 master 进程和多个相互独立的 worker 进程. 2. 每个子进 ...

  4. vue2.0子组件修改父组件props数据的值

    从vue1.0升级至2.0之后 prop的.sync被去除 因此直接在子组件修改父组件的值是会报错的如下: 目的是为了阻止子组件影响父组件的数据那么在vue2.0之后 如何在子组件修改父组件props ...

  5. PHP html mysql js 乱码问题,UTF-8(乱码)

    一.HTML页面转UTF-8编码问题 1.在head后,title前加入一行: <meta http-equiv='Content-Type' content='text/html; chars ...

  6. bzoj 2091 The Minima Game - 动态规划 - 博弈论

    题目传送门 需要验证权限的传送门 题目大意 Alice和Bob轮流取$n$个正整数,Alice先进行操作.每次每人可以取任意多的数,得分是这一次取的所有数中的最小值.Alice和Bob都足够聪明,他们 ...

  7. Codeforces 789D Weird journey - 欧拉路 - 图论

    Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...

  8. Codeforces Round #427 (Div. 2) Problem B The number on the board (Codeforces 835B) - 贪心

    Some natural number was written on the board. Its sum of digits was not less than k. But you were di ...

  9. How to use Junit Listener

    JUnit Listeners If you want to do some operations when your tests are started, passed, finished, fai ...

  10. Python3基础 list in/not in 判断一个变量是否在列表中存在

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...