问题
SSM中jsp页面表单中input里面怎么显示‘yyyy-mm-dd’日期格式并提交。
原因
input里text是String类型 ,若数据为Date类型提交时则会出错。
解决
在jsp页面中的显示’‘yyyy-mm-dd’’
在jsp页面开头添加
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
在表单中加入下面语句即可显示’‘yyyy-mm-dd’'的设置时间
<td><fmt:formatDate value="${row.settime}" pattern="yyyy-MM-dd"/></td>
在jsp页面中的显示与提交
下面语句可以在表单显示需要提交的’‘yyyy-mm-dd’'的开始时间
<td><input type="text" value="<fmt:formatDate value="${row.begindate}" pattern="yyyy-MM-dd"/>" name="begindate" > </td>
但需要先在Controller中添加
/**
* 数据初始化绑定:SpringMVC中,数据从前台过来会将字符串转换为日期对象
* @param binder
*/
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//格式根据个人需求进行设定
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); //true:允许输入空值,日期可以为空,false:不能为空值
}
再在spring-mvc.xml添加
<!-- 使用注解驱动 -->
<mvc:annotation-driven/>
提交即可解决
若无法解决可以评论我哦!