赞
踩
前端为layui实现的表单,有一个时间选项,提交到后台时,发现对象的时间字段一直为Null,现在给出解决办法。我的前端页面是这样的:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加上岗证信息</title> <link rel="stylesheet" href="/layui/css/layui.css" media="all"> </head> <body style="padding: 20px"> <form id="form" class="layui-form layui-form-pane"> <table class="editTable" style="width:100%;"> <tr> <td class="label">*用户名:</td> <td> <input type="text" name="username" id="username" required lay-verify="required|usernameLen" placeholder="请输入用户名" autocomplete="off" class="layui-input"> </td> <td class="label">已取得上岗证专业:</td> <td> <input type="text" name="major" id="major" required lay-verify="required|majorLen" placeholder="请输入已取得上岗证专业" autocomplete="off" class="layui-input"> </td> </tr> <tr> <td class="label">已取得上岗证时间:</td> <td> <input type="text" name="rTime" id="rTime" autocomplete="off" class="layui-input"> </td> <td class="label">已取得上岗证等级:</td> <td> <div class="sel"> <select id="level" name="level"> <option value="">请选择上岗证等级</option> <option value="初级">初级</option> <option value="中级">中级</option> <option value="高级">高级</option> </select> </div> </td> </tr> <tr> <td class="label">已取得上岗证编号:</td> <td> <input type="text" name="cardNO" id="cardNO" required lay-verify="required|cardLen" placeholder="请输入已取得上岗证编号" autocomplete="off" class="layui-input"> </td> <td class="label">备注:</td> <td> <input type="text" name="intro" id="intro" placeholder="请输入备注" lay-verify="usernameLen" autocomplete="off" class="layui-input"> </td> </tr> </table> <div class="layui-form-item" style="margin-top: 10px;margin-left: 10px;text-align: center"> <button class="layui-btn" lay-submit="" lay-filter="addCard">提交</button> <button type="reset" id="res" class="layui-btn layui-btn-primary" style="margin-left: 20px;background-color: #FF5722">重置</button> </div> </form> <script type="text/javascript" src="/layui/layui.js"></script> <script type="text/javascript"> layui.use(['form','layer','layedit','laydate','upload','table'],function(){ let form = layui.form, layer = parent.layer === undefined ? layui.layer : top.layer, laypage = layui.laypage, upload = layui.upload, layedit = layui.layedit, laydate = layui.laydate, $ = layui.jquery, table = layui.table; //自定义校验 form.verify({ usernameLen:function(value){ if(value.length>20) return "长度不能超过20"; }, majorLen:function(value){ if(value.length>50) return "长度不能超过50"; }, cardLen:function(value){ if(value.length>13) return "长度不能超过13"; }, stationLen:function (value) { if(value.length>10) return "长度不能超过10"; } }); checkPermission();//该方法用来将不具备该权限的html元素隐藏 //日期选择器 laydate.render({ elem: '#rTime' //,type: 'month' }); //提交表单 form.on('submit(addCard)', function(data){ // let formdata = $('#form').serializeObject(); // console.log('formdata:',formdata); let formdata=data.field; console.log('formdata:',formdata); $.ajax({ url: '/card/addCard', data: JSON.stringify(formdata), type: "post", dataType: "json", contentType:"application/json; charset=utf-8", success: function (res) { if(res.code==="200"){ layer.alert('添加上岗证信息成功', { icon: 1, skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则,去这里查阅 }); $("#res").click(); } } }); return false; }); }); </script> </body> </html>
如上图所示,rTime字段是选择时间的,后端控制层的代码为:
@RestController @RequestMapping("/card") public class CardController { @Resource private CardService cardService; private static final Logger log = LoggerFactory.getLogger("adminLogger"); @PostMapping("/addCard") public ResponseData addCard(@RequestBody CardInfo cardInfo){ System.out.println(cardInfo.toString()); ResponseData rd=new ResponseData(); try { cardService.addCard(cardInfo); rd.setCode("200"); }catch (Exception ex){ rd.setCode("404"); ex.printStackTrace(); } return rd; } }
CardInfo结构为:注意rTime字段上面的注解,必须加这个才能识别到前端传过来的时间格式参数
import com.fasterxml.jackson.annotation.JsonFormat; import com.smp.security.model.BaseEntity; import lombok.Getter; import lombok.Setter; import lombok.ToString; import java.util.Date; @Getter @Setter @ToString public class CardInfo extends BaseEntity<Long> { private static final long serialVersionUID = 6619894919257921520L; private String username;//用户名 private String major;//上岗证的专业 @JsonFormat(pattern="yyyy-MM-dd")//这里很关键,不加的话,date类型遍历得到的值为null private Date rTime;//获取上岗证的时间 private String level;//上岗证等级 private String cardNO;//上岗证编号 private String intro;//备注 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。