天赐我爱国语版,公猫发情,曹志安
最近在做vue+element-ui的后台管理页面,其中用到了datetimepicker来选择日期时间,但是在将数据传回后台的过程中遇到了一些令人头疼的问题,在此记录一下解决方案,以免日后再次遇到。
前端页面
前端代码
submitform(formname) { this.$refs[formname].validate((valid) => { let url = 'http://localhost:8088/pethospital/order-record' let data = qs.stringify({ title: this.orderform.title, hospitalid: this.orderform.hospitalid, orderdate: this.orderform.orderdate, ordertype: this.orderform.ordertype, petvariety: this.orderform.petvariety, mobilephone: this.orderform.mobilephone, supplement: this.orderform.supplement }) if (valid) { axios.post(url, data) .then(response => { }).catch(error => { this.$message({ message: '错误:' + error, type: true }) }) } else { this.$message('验证错误:请确认信息是否填写完整') } }); }
实体类代码
private long id; private string title; private integer hospitalid; private date orderdate; private integer ordertype; private string petvariety; private string mobilephone; private string supplement;
controller代码
@postmapping("/order-record") public commonresult addorderrecord(orderrecorddo orderrecorddo) throws parseexception { system.out.println("添加的预约记录:" + orderrecorddo); orderrecorddomapper.insertselective(orderrecorddo); return null; }
控制台输出
field error in object 'orderrecorddo' on field 'orderdate': rejected value [2019-04-10 10:00:00]; codes [typemismatch.orderrecorddo.orderdate,typemismatch.orderdate,typemismatch.java.util.date,typemismatch]; arguments [org.springframework.context.support.defaultmessagesourceresolvable: codes [orderrecorddo.orderdate,orderdate]; arguments []; default message [orderdate]]; default message [failed to convert property value of type 'java.lang.string' to required type 'java.util.date' for property 'orderdate'; nested exception is org.springframework.core.convert.conversionfailedexception: failed to convert from type [java.lang.string] to type [java.util.date] for value '2019-04-10 10:00:00'; nested exception is java.lang.illegalargumentexception]]
看了控制台的输出信息,大概知道是前端将日期当做string类型传输的,但是我们后台定义日期用的是date类型,因此这里报的转换异常。本来我想用simpledateformat来转换的,但是觉得这样很麻烦,然后在网上查找相关资料发现可以有更简单的方法。
尝试1:
在实体类字段上添加@datetimeformat(pattern = "yyyy-mm-dd hh:mm:ss")
@datetimeformat(pattern = "yyyy-mm-dd hh:mm:ss") private date orderdate;
控制台输出
添加的预约记录:{"id":null,"title":"测试1","hospitalid":1001,"orderdate":"wed apr 10 10:00:00 cst 2019","ordertype":2001,"petvariety":"哈士奇","mobilephone":"1000","supplement":"二哈"}
数据库记录
数据库记录
遇到的问题:从数据库获取数据后在前端显示不友好
显示
尝试2: 在实体类字段添加@datetimeformat(pattern = "yyyy-mm-dd hh:mm:ss")和@jsonformat(pattern = "yyyy-mm-dd hh:mm:ss", timezone = "gmt+8")
/** * timezone = "gmt+8"指定时区 */ @datetimeformat(pattern = "yyyy-mm-dd hh:mm:ss") @jsonformat(pattern = "yyyy-mm-dd hh:mm:ss", timezone = "gmt+8") private date orderdate;
前端显示效果:这下就能显示成我们想要的效果了
前端显示
尝试3:我的后台项目使用springboot搭建的,我在application.yml文件中添加如下配置
# 配置数据源 spring: datasource: name: pet-hospital type: com.alibaba.druid.pool.druiddatasource url: jdbc:mysql://localhost:3306/pet_hospital?servertimezone=gmt%2b8 driver-class-name: com.mysql.cj.jdbc.driver username: root password: 1741248769 # vue前端传来的日期为string类型,下面的设置可以自动将其转换为date类型,不需要手动转换 mvc: date-format: yyyy-mm-dd hh:mm:ss # 以下设置可以将date类型自动转换为如下格式的日期,指定jackson格式化日期使用的时区,jackson默认使用utc jackson: date-format: yyyy-mm-dd hh:mm:ss time-zone: gmt+8
显示效果
显示
总结:
附:原时间2019-04-12 12:00:00,相差8个小时
不指定时区
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
VUE+elementui组件在table-cell单元格中绘制微型echarts图
Vue通过getAction的finally来最大程度避免影响主数据呈现问题
vue 路由懒加载中给 Webpack Chunks 命名的方法
网友评论