本文共 3198 字,大约阅读时间需要 10 分钟。
前台jsp用的一个日期插件,后台获取一直有问题。
被这个问题搞了好久,其实很简单。记录下来,希望可以帮到遇到同样问题的同学。 我项目使用的ssm框架, 在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置。 1、如果查询类是我们自己写,那么在属性前面加上@DateTimeFormat(pattern = "yyyy-MM-dd") ,即可将String转换为Date类型,如下 @DateTimeFormat(pattern = "yyyy-MM-dd") private Date createTime; 2、如果我们只负责web层的开发,就需要在controller中加入数据绑定: 1 2 3 4 5 6 |
|
3、可以在系统中加入一个全局类型转换器实现转换器,新建一个controller
1 2 3 4 5 6 7 8 9 10 11 12 |
|
在springmvc配置文件中进行配置:
1 2 3 4 5 6 7 8 9 |
|
我使用了第三种方式,但在运行的时候报错,最后发现是DateConverter类中的日期转换有问题,
debug发现前台传过来的是一串数字,猜测应该是毫秒,然后就在DateConverter类中将接受的source先进行了毫秒转成日期格式的时间,在进行转换结果没报错但日期还是不对,最后猜测前台传过来的应该是秒,debug将穿过来的日期记下来,用计算器转换发现确实是秒(这日期插件 --!!!一开始没想到传过来的时间是秒..算是个坑吧!)。问题找到了,剩下的就是日期转换的问题了( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
好了,基本就是这样了。这个问题关键在于前台传过来的居然是秒,搞了半天....
转载地址:http://rqwxi.baihongyu.com/