1.时间数据提交
在使用SpringMVC获取表单数据时,获取的时间是字符串格式,当名称和接收类中的时间名称一致但无法实现赋值时就会出错,关键是出错后可能看不到任何的提示,在方法测试显示未调用前端一直显示已经调用,这是时间格式问题
需要配置自定义时间转换器或者使用系统默认的时间转换器
这里展示自定义时间转换
创建装换其类
public class CustomDateConverter implements Converter<String, Date>{
@Override
public Date convert(String source) {
// 将日期串转换成日期格式(格式是yyyy-MM-dd)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
//返回上述格式的Date类型对象
return simpleDateFormat.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
在springmvc配置文件中添加配置项
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<!-- 日期类型的转换 -->
<set>
<bean class="com.xj.utils.CustomDateConverter" />
</set>
</property>
</bean>
2.时间数据显示
如果前端使用的是jsp使用EL标签可以对时间进行格式化显示
引入标签库
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
显示
<fmt:formatDate value="${row.date}" pattern="yyyy-MM-dd"/>