前端页面
<form class="modify_pwd" action="admin/change/pass" method="post" id="change_pwd"> <table width="100%"> <tr><td><input id="admin_id" value="${admin.id}" name="id" type="hidden"/></td></tr> <tr> <td class="need" style="width:10px;">*</td> <td style="width:100px;">原密码:</td> <td style="width:210px;"> <input type="password" value="" name="oldpwd" class="inputxt" ajaxurl="admin/check/pass/${admin.id}" datatype="*6-18" errormsg="密码至少6个字符,最多18个字符!" /> </td> <td> <div class="Validform_checktip" >密码至少6个字符,最多18个字符!</div> </td> </tr> <tr> <td class="need" style="width:10px;">*</td> <td style="width:100px;">新密码:</td> <td style="width:210px;"> <input type="password" id="newPwd" value="" name="password" class="inputxt" plugin="passwordStrength" datatype="*6-18" errormsg="密码至少6个字符,最多18个字符!" /> </td> <td > <div class="Validform_checktip" >密码至少6个字符,最多18个字符!</div> <div class="passwordStrength" style="display:none;"><b>密码强度:</b> <span>弱</span><span>中</span><span class="last">强</span></div> </td> </tr> <tr> <td class="need">*</td> <td style="width:100px;">确认密码:</td> <td style="width:210px;"><input type="password" value="" name="repassword" class="inputxt" recheck="password" datatype="*6-18" errormsg="两次输入的密码不一致!" /></td> <td><div class="Validform_checktip">两次输入的密码需一致</div></td> </tr> <tr> <td class="need"></td> <td style="padding:10px 0 18px 0;"> <input class="btn btn-primary " id="btn_sub" type="submit" value=" 提交 "/> </td> <td style="padding:10px 0 18px 0;"> <input class="btn btn-warning " id="btn_reset" type="reset" value=" 重 置 " /> </td> </tr> </table> </form>
验证原密码后台代码
/* *service方法,返回数据一定要是这种格式{"status":"y","info":"成功"} *{"status":"n","info":"失败"} */ public Map<String, Object> checkPass(int id, String oldPwd) { TbAdmin admin = tbAdminMapper.selectByPrimaryKey(id); Map<String, Object> map=new HashMap<>(); if(admin!=null) { String oldPassword = admin.getPassword(); if(MD5Utils.md5(oldPwd).equals(oldPassword)) { map.put("status", "y"); return map; }else { map.put("status", "n"); map.put("info", "密码错误"); return map; } }else { map.put("status", "n"); map.put("info", "密码错误"); return map; } /* *controller *参数名称要写param */ @RequestMapping("/check/pass/{id}") @ResponseBody public Map<String, Object> checkPassword(@PathVariable Integer id,String param) { Map<String, Object> result = adminService.checkPass(id, param); return result; }
提交表单js代码
$("#change_pwd").Validform({ btnSubmit : "#btn_sub", tiptype : 2, ajaxPost:true, showAllError : false, postonce : true, usePlugin:{ passwordstrength:{ minLen:6,//设置密码长度最小值,默认为0; maxLen:18,//设置密码长度最大值,默认为30; trigger:function(obj,error){ if(error){ obj.parent().next().find(".Validform_checktip").show(); obj.parent().next().find(".passwordStrength").hide(); }else{ obj.parent().next().find(".Validform_checktip").hide(); obj.parent().next().find(".passwordStrength").show(); } } } }, callback:function(data){ if(data.status=="y"){ $.Hidemsg(); //此处有个小问题,不想要自带的弹窗(tiptype=4不显示那个弹窗,但是别的样式太丑)但是又不会改自带的样式,会闪一下那个提示窗,谁有好的方法,请告知,谢谢。 layer.msg(data.info, {icon:1,time: 1000}, function(){ location.reload();//刷新页面 }); }else{ $.Hidemsg(); layer.msg(data.info, {icon:5,time: 3000}); } var index =parent.$("#iframe").attr("src"); parent.layer.close(index); } });
提交表单后台代码
//service 返回数据格式同上 public Map<String, Object> updatePass(int id, String password) throws Exception { TbAdmin admin=new TbAdmin(); String pwd = MD5Utils.md5(password); admin.setId(id); admin.setPassword(pwd); Map<String, Object> map=new HashMap<>(); try { tbAdminMapper.updateByPrimaryKeySelective(admin); map.put("status", "y"); map.put("info", "修改成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(); } return map; } //controller @RequestMapping("/change/pass") @ResponseBody public Map<String, Object> changePassword( Integer id,String password) { Map<String, Object> map=new HashMap<>(); try { map = adminService.updatePass(id, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); map.put("status", "n"); map.put("info", "修改失败"); } return map; }