ssm项目:找回密码

用的ajax书写的

  • Controller层
/**
     * 跳转到更新个人密码页面
     * Created by gf on 2017年7月19日
     * 
     * @return
     */
    @RequestMapping(value="password.do",method=RequestMethod.GET)
    public String updatePwdGet(HttpSession session,Model model){
        //根据当前用户查询基本信息,然后返回个人中心页面
        UserModel user = getCurrentUser(session);

        model.addAttribute("user",user);

        return "front/user/password";
    }

    /**
     * 提交并更新个人密码
     * Created by gf on 2017年7月19日
     * 
     * @return
     */
    @RequestMapping(value="password.do",method=RequestMethod.POST)
    public @ResponseBody
    Object updatePwdPost(UpdatePwdInfo info,Model model,HttpSession session){
        UserModel user = getCurrentUser(session);

        //判断旧密是否正确,不正确禁止更新
        //此处未做数据校验
        OperateResult result = userService.updateUserPassword(user.getId(),info);
        //判断是否更改成功,根据结果给出不同的提示信息

        HashMap map = new HashMap();
        if(!result.isSuccess()){
            System.out.println(">>>>>重置密码失败:"+result.getMessage());
            map.put("success", false);
            map.put("message", result.getMessage());
            return map;
        }else{
            map.put("success", true);
            map.put("message", result.getMessage());
            return map;
        }
    }

其中UpdatePwdInfo是封装的一个参数对象,代码如下


/**
 * Created By gf on 2017年7月20日
 * Descr: 更新密码时提交的信息
 *
 */
public class UpdatePwdInfo {

    private String oldPassword;
    private String newPassword;
    private String newPasswordAgain;
    public String getOldPassword() {
        return oldPassword;
    }
    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }
    public String getNewPassword() {
        return newPassword;
    }
    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }
    public String getNewPasswordAgain() {
        return newPasswordAgain;
    }
    public void setNewPasswordAgain(String newPasswordAgain) {
        this.newPasswordAgain = newPasswordAgain;
    }
    @Override
    public String toString() {
        return "UpdatePwdInfo [oldPassword=" + oldPassword + ", newPassword=" + newPassword + ", newPasswordAgain="
                + newPasswordAgain + "]";
    }

}

OperateResult是封装的结果

/**
 * Created By gf on 2017年7月19日
 * Descr: 封装操作结果
 *
 */
public class OperateResult {

    private boolean success;
    private String message;
    public boolean isSuccess() {
        return success;
    }
    public void setSuccess(boolean success) {
        this.success = success;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    @Override
    public String toString() {
        return "OperateResult [success=" + success + ", message=" + message + "]";
    }


}
  • service层实现类
@Override
    public OperateResult updateUserPassword(int id,UpdatePwdInfo info) {
        UserModel model = queryUserById(id);
        OperateResult result = new OperateResult();
        if(model.getPassword().equals(MD5Utils.getMd5Simple(info.getOldPassword()))){
            HashMap map = new HashMap();
            map.put("id", id);
            map.put("pwd", MD5Utils.getMd5Simple(info.getNewPassword()));
            mapper.updateUserPwd(map);
            result.setSuccess(true);
            result.setMessage("更新成功");
        }else{
            result.setSuccess(false);
            result.setMessage("旧密码输入错误");
        }
        return result;
    }
  • mapper层
public int updateUserPwd(HashMap map);
  • xml映射文件

  <update id="updateUserPwd" parameterType="java.util.HashMap">
    update user set password=#{pwd} where id=#{id}
  update>
  • jsp页面
    使用了ajax
<body class="w100">
   <jsp:include page="uheader.jsp">jsp:include>
    <main>
        <div class="container">
            <h2>我的资料h2>
            <div id="profile_tab">
                <ul class="profile_tab_header f_left clearfix">
                    <li><a href="front/user/profile.do">更改资料a>li>
                    <li class="profile_tab_line">|li>
                    <li><a href="front/user/avatar.do">更改头像a>li>
                    <li class="profile_tab_line">|li>
                    <li><a href="front/user/password.do">密码安全a>li>
                ul>
                <div class="proflle_tab_body">
                    <div class="proflle_tab_workplace clearfix">
                        <div class="profile_avatar_area">

                           <c:if test="${empty user.headUrl}">
                                 <img id="avatar"  src="static/img/avatar_lg.png" alt="">
                              c:if>

                              <c:if test="${not empty user.headUrl}">
                                 <img id="avatar" width="200px" height="200px" src="/pic/${user.headUrl}" alt="">
                              c:if>

                        div>

                        <div class="profile_ifo_area">

                         <c:if test="${not empty message}">
                            <div>
                                <strong>${message}strong>
                            div>
                            c:if>
                            <form   id="password_form">
                                <div class="form_group">
                                    <span class="dd">旧 密 码:span>
                                    <input type="password" id="oldPassword" name="oldPassword">
                                div>
                                <div class="form_group">
                                    <span class="dd">新 密 码:span>
                                    <input type="password" id="newPassword"  name="newPassword">
                                div>
                                <div class="form_group">
                                    <span class="dd">确认新密码:span>
                                    <input type="password" id="newPassword02" name="newPasswordAgain">
                                div>
                                <div class="form_submit dd">
                                    <input type="submit" onclick="password()" value="保 存">
                                div>
                            form>
                        div>
                    div>
                div>
            div>
        div>
    main>
    <jsp:include page="ufooter.jsp">jsp:include>
    <%@include file="../include/script.html"%>

   <script type="text/javascript">
   function password() {
        $.post('front/user/password.do',$('#password_form').serialize(),function(result){
            console.log(result);
            if(result.success){

                alert(result.message);
                location.reload();
            }else{
                alert(result.message);
            }
        },'json');
    }




   script>
body>

你可能感兴趣的:(java)