学生考试项目使用SSM框架遇到的问题及解决方法
主要是在Date类型数据传输问题
描述:
Date数据从数据库中拿到的数据为格林威治时间格式,当它输出到页面是也编程格林威治时间格式,到进行修改那一步时发现格林威治时间在修改页面变为字符串无法被接受到,然后就页面中就使用到了
<%--加在头文件位置,如下图--%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
使用方法:
<fmt:formatDate value='${list.startTime}' pattern='yyyy-MM-dd HH:mm:ss'/>
在修改页面将这一串放在<input>的value值中,如下图:
这是前端页面的,后台就需要使用到注解或者使用工具类(两者不能同时使用)
首先时注解的方式,在Date类型的数据上加
@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)
@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”,timezone=“GMT+8”)
工具类:
使用工具类参考此链接https://www.cnblogs.com/zhu520/p/7905922.html
package com.stuexam.controller;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BaseController implements Converter<String,Date> {
/*@InitBinder//初始化绑定,所有数据提交之前都要经过它
public void initBinder(ServletRequestDataBinder binder){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
*//**
* 通过重新构造客户端内容
* 寻找与实体类中Date.class属性名和参数名称相同的数据
* 再通过sdf的方式进行转换为Date
*//*
binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));
}*/
@Override
public Date convert(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return simpleDateFormat.parse(s);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
还有一个注意的地方,就时jsp页面上传值的name属性,其属性值必须要使用实体类中相同的属性名,如果不一致会报错,列名不明确或不存在。
我暂时只遇到这个问题,当时改了挺久的,自己百度,然后自己琢磨出来的。