从下都上
UserMapper
//更新密码
int updatePassword(int id,String password);
user-mapper.xml
<update id="updatePassword">
update user set password = #{password} where id = #{id}
</update>
UserService
//个人设置修改密码功能
public Map<String,Object> updatePassword(String password,String newPassword,int id){
Map<String,Object> map =new HashMap<>();
User user = userMapper.selectByID(id);
password= CommunityUtil.md5(password+user.getSalt());
if(!user.getPassword().equals(password)){
map.put("passwordMsg","输入密码错误!");
return map;
}
else {//注意:存入新密码要以加密后的形式存进去
newPassword= CommunityUtil.md5(newPassword+user.getSalt());
userMapper.updatePassword(id,newPassword);
}
return map;
}
UserController
//个人设置页面修改密码功能
//这里形参用Model类和User类即可,SpringMVC会把传入内容按照User属性填入user
@RequestMapping(path = "/setting", method = RequestMethod.POST)
public String updatePassword(Model model, String password,String newPassword,String confirmPassword) {
if(StringUtils.isBlank(password)){
model.addAttribute("passwordMsg","请输入原始密码!");
return "/site/setting";
}
if(StringUtils.isBlank(newPassword)){
model.addAttribute("newPasswordMsg","请输入新密码!");
return "/site/setting";
}
if(StringUtils.isBlank(confirmPassword)){
model.addAttribute("confirmPasswordMsg","请再次输入新密码!");
return "/site/setting";
}
if(!confirmPassword.equals(newPassword)){
model.addAttribute("newPasswordMsg","两次输入的新密码不相同!");
return "/site/setting";
}
User user=hostHolder.getUser();
Map<String, Object> map = userService.updatePassword(password,newPassword,user.getId());
if (map == null || map.isEmpty()) {
//传给templates注册成功信息
model.addAttribute("msg", "密码修改成功");
//跳到回个人设置页面
model.addAttribute("target", "/user/setting");
return "/site/operate-result";
}else {
//失败了传失败信息,跳到到原来的页面
model.addAttribute("passwordMsg","输入的原始密码错误!");
return "/site/setting";
}
}
setting.html:注意form标签的要加method th:action
<!-- 修改密码 -->
<h6 class="text-left text-info border-bottom pb-2 mt-5">修改密码</h6>
<form class="mt-5" method="post" th:action="@{/user/setting}">
<div class="form-group row mt-4">
<label for="old-password" class="col-sm-2 col-form-label text-right">原密码:</label>
<div class="col-sm-10">
<input type="password"
th:class="|form-control ${passwordMsg!=null?'is-invalid':'' }|"
id="old-password"
th:value="${param.password}"
name="password" placeholder="请输入原始密码!" required>
<div class="invalid-feedback"th:text="${passwordMsg}">
密码长度不能小于8位!
</div>
</div>
</div>
<div class="form-group row mt-4">
<label for="new-password" class="col-sm-2 col-form-label text-right">新密码:</label>
<div class="col-sm-10">
<input type="password"
th:class="|form-control ${newPasswordMsg!=null?'is-invalid':'' }|"
id="new-password"
th:value="${param.newPassword}"
name="newPassword" placeholder="请输入新的密码!" required>
<div class="invalid-feedback"th:text="${newPasswordMsg}">
密码长度不能小于8位!
</div>
</div>
</div>
<div class="form-group row mt-4">
<label for="confirm-password" class="col-sm-2 col-form-label text-right">确认密码:</label>
<div class="col-sm-10">
<input type="password"
th:class="|form-control ${confirmPasswordMsg!=null?'is-invalid':'' }|"
id="confirm-password"
th:value="${param.confirmPassword}"
name = confirmPassword placeholder="再次输入新密码!" required>
<div class="invalid-feedback">
两次输入的密码不一致!
</div>
</div>
</div>