SRPINGMVC+EASYUI前端提交DATE类型字段报错

fxxzfj 2017-02-09 12:32:08
日常都是用公司的框架,最近无聊学习下外部公用框架,结果遇到个问题
情况如下:
项目用MAVEN搭建,SPRINGMVC+MYBATIS+EASYUI
问题如下:我的表里有一个字段是DATE类型,前端往表里插数据的时候,直接报错了
截图如下

我查了下,应该是我页面获取EASYUI时间得到的是字符串,例如2017-02-09这样的,但是这个字符串在转换成我实体类中的对应DATE字段时候出错了
前端代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path ;
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>客户管理</title>
<!-- 引入JQuery -->
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.5.1/jquery.min.js"></script>
<!-- 引入easyloader.js -->
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.5.1/easyloader.js"></script>
<!-- 引入EasyUI -->
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<!-- 引入EasyUI的中文国际化js,让EasyUI支持中文 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.5.1/locale/easyui-lang-zh_CN.js"></script>
<!-- 引入EasyUI的样式文件-->
<link rel="stylesheet" href="${pageContext.request.contextPath}/jquery-easyui-1.5.1/themes/default/easyui.css" type="text/css"/>
<!-- 引入EasyUI的图标样式文件-->
<link rel="stylesheet" href="${pageContext.request.contextPath}/jquery-easyui-1.5.1/themes/icon.css" type="text/css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/common/json2.js"></script>
<script type="text/javascript">
//保存
function saveUser(){
var v = $('#dBdate').datebox('getValue');
alert(1);
var d = new Date(v);
alert(d);
$('#fm').form('submit',{
url: '<%=basePath%>/customerController/add.do',
onSubmit: function(){
//if ($(this).form("validate")) {
// return true;
//} else {
// return false;
//}
},
success: function(data){
var temp = $.parseJSON(data);
alert(temp.success);
if(temp.success=="true") {
alert(1);
$.messager.alert('保存成功',temp.msg,'Info');
}else{
alert(2);
$.messager.alert('保存失败',temp.msg,'Info');
}
$('#add').dialog('close'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
});

}

</script>
</head>
<body>
<div id="add" class="easyui-dialog" style="width:800px" buttons="#add-buttons">
<form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
<div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">客户信息</div>
<div style="margin-bottom:10px">
<input name="cBs" class="easyui-textbox" required="true" label="客户ID:" style="width:49%">
<input name="cName" class="easyui-textbox" required="true" label="客户昵称:" style="width:49%">
</div>
<div style="margin-bottom:10px">
<input name="cRealname" class="easyui-textbox" required="true" label="客户姓名:" style="width:49%">
<input name="cTel" class="easyui-textbox" required="true" label="电话:" style="width:49%">
</div>
<div style="margin-bottom:10px">
<input name="cQq" class="easyui-textbox" label="QQ:" style="width:49%">
<input name="cWx" class="easyui-textbox" label="微信:" style="width:49%">
</div>
<div style="margin-bottom:10px">
<input name="cWw" class="easyui-textbox" label="淘宝旺旺ID:" style="width:49%">
<select class="easyui-combobox" name="cLevel" label="客户等级:" style="width:49%">
<option value="LEVEL1">LEVEL1</option>
<option value="LEVEL2">LEVEL2</option>
<option value="LEVEL3" selected>LEVEL3</option>
<option value="LEVEL4">LEVEL4</option>
<option value="LEVEL5">LEVEL5</option>
</select>
</div>
<div style="margin-bottom:10px">
<input name="dBdate" id="dBdate" class="easyui-datebox" label="生日:" style="width:49%">
<select class="easyui-combobox" name="cFkjs" label="是否分开结算:" style="width:49%">
<option value="是">是</option>
<option value="否" selected>否</option>
</select>
</div>
<!--div style="margin-bottom:10px">
<input name="cAddress1" class="easyui-textbox" label="主地址:" style="width:100%">
</div>
<div style="margin-bottom:10px">
<input name="cAddress2" class="easyui-textbox" label="备用地址1:" style="width:100%">
</div>
<div style="margin-bottom:10px">
<input name="cAddress3" class="easyui-textbox" label="备用地址2:" style="width:100%">
</div>
<div style="margin-bottom:10px">
<input name="cAddress4" class="easyui-textbox" label="备用地址3:" style="width:100%">
</div>
<div style="margin-bottom:10px">
<input name="cAddress5" class="easyui-textbox" label="备用地址4:" style="width:100%">
</div-->
</form>
</div>
<div id="add-buttons">
<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">保存</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#add').dialog('close')" style="width:90px">取消</a>
</div>
</body>
</html>
后端代码如下:
//新增客户
@RequestMapping("/add.do")
@ResponseBody
public Object add(Tcustomer model){
int k=0;
try{
k= this.cusService.insertCustomer(model);
if(k>=0){
System.out.println("k>0");
}else{
System.out.println("k<0");
}
}catch(DataAccessException e){
k = -1;
System.out.println("异常捕获");
System.out.println(e.toString());
}
Map<String,String> map = new HashMap<String,String>();
if(k>0){
map.put("success", "true");
map.put("msg", "添加成功!");
}else{
map.put("success", "false");
map.put("msg", "添加失败!");
}
return map;
}
实体类定义:
public class Tcustomer {

@DateTimeFormat(pattern="yyyy-MM-dd")


@DateTimeFormat(pattern="yyyy-MM-dd")
public Date getdBdate() {
return dBdate;
}

@DateTimeFormat(pattern="yyyy-MM-dd")
public void setdBdate(Date dBdate) {
this.dBdate = dBdate;
}
}
备注下:
我查了很多解决方法,都是推荐把DATE转为Strig类型,然后在存储的时候,使用to_date方法保存,这个确实可以解决,但是我觉得应该是可以直接转DATE的,有没有不用改类型的方法呢?
...全文
314 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fxxzfj 2017-02-20
  • 打赏
  • 举报
回复
感谢诸位的帮助,后来我自己解决了,主要还是SPRING MVC不熟悉导致的 方法上是要再实体类定义的时候添加 @DateTimeFormat(pattern="yyyy-MM-dd") 例如: @DateTimeFormat(pattern="yyyy-MM-dd") private Date dBdate; 此方法可以对前后端的该类别定义的属性字段自动进行转义为指定格式的DATE, 然后在实现类的GET跟SET方法中 添加如下 @DateTimeFormat(pattern="yyyy-MM-dd") @JsonFormat(pattern="yyyy-MM-dd") public Date getdBdate() { System.out.println("getdate"); return dBdate; } @DateTimeFormat(pattern="yyyy-MM-dd") @JsonFormat(pattern="yyyy-MM-dd") public void setdBdate(Date dBdate) { System.out.println("setdate"); this.dBdate = dBdate; } 其中的JSONFORMAT用来指定传送到前端的显示的类型为YYYY-MM--DD格式
fxxzfj 2017-02-16
  • 打赏
  • 举报
回复
引用 6 楼 cjbedu2012 的回复:
[quote=引用 4 楼 fxxzfj 的回复:] [quote=引用 1 楼 cjbedu2012 的回复:] 1、将实体类中的日期字段加上一句@dateformat 格式自己定 2、jsp传入时的格式需和后台设置一样
实体类里面已经加了@DateTimeFormat(pattern="yyyy-MM-dd") 传入的格式也确认是对的[/quote] 1、把get和set上面的@dateTimeFormat去掉,只需在字段上加 2、将提交的form参数序列化下[/quote] 我如果去掉GET跟SET上面的这个,在显示时候就会显示时间的LONG型
cjbedu2012 2017-02-15
  • 打赏
  • 举报
回复
引用 4 楼 fxxzfj 的回复:
[quote=引用 1 楼 cjbedu2012 的回复:] 1、将实体类中的日期字段加上一句@dateformat 格式自己定 2、jsp传入时的格式需和后台设置一样
实体类里面已经加了@DateTimeFormat(pattern="yyyy-MM-dd") 传入的格式也确认是对的[/quote] 1、把get和set上面的@dateTimeFormat去掉,只需在字段上加 2、将提交的form参数序列化下
fxxzfj 2017-02-14
  • 打赏
  • 举报
回复
引用 3 楼 qq_25543635 的回复:
后台用model方式接收前端字段
具体写法有吗?我这边用的是 @RequestMapping("/add.do") @ResponseBody public Object add(Tcustomer model){ } Tcustomer是我的实体类
fxxzfj 2017-02-14
  • 打赏
  • 举报
回复
引用 1 楼 cjbedu2012 的回复:
1、将实体类中的日期字段加上一句@dateformat 格式自己定 2、jsp传入时的格式需和后台设置一样
实体类里面已经加了@DateTimeFormat(pattern="yyyy-MM-dd") 传入的格式也确认是对的
那年花 2017-02-09
  • 打赏
  • 举报
回复
后台用model方式接收前端字段
Leonis丶L 2017-02-09
  • 打赏
  • 举报
回复
是不是前端没有设定ajax接收的返回值类型。你后台传的对象前台默认接收的是字符串所以报错了。。。。。 我也不知道,我也在学spring mvc
cjbedu2012 2017-02-09
  • 打赏
  • 举报
回复
1、将实体类中的日期字段加上一句@dateformat 格式自己定 2、jsp传入时的格式需和后台设置一样

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧