1차 프로젝트
패스워드 변경 수정
Controller
@GetMapping("/changePwd.do")
public String changePwd1() {
return "changePwdForm";
}
@PostMapping("/changePwd.do")
@ResponseBody
public String changePassword(@RequestParam String curPwd, @RequestParam String newPwd, HttpSession session) {
User user = (User) session.getAttribute("authUser"); // 로그인된 사용자 정보
Member storedMember = memberDao.selectById(user.getId());
if (!storedMember.matchPassword(curPwd)) {
return "badCurPwd"; // 현재 비밀번호 불일치 시 에러 반환
}
try {
changePwdSvc.changePassword(user.getId(), curPwd, newPwd);
return "success"; // 비밀번호 변경 성공 시 성공 반환
} catch (InvalidPasswordException e) {
return "error"; // 비밀번호 변경 실패 시 에러 반환
} catch (MemberNotFoundException e) {
return "error"; // 사용자 정보를 찾을 수 없을 때 에러 반환
}
}
자바스크립트 ( Ajax )
$(document).ready(function() {
$("#changePw").click(function(event) {
event.preventDefault();
var curPwd = $("#curPwd").val();
var newPwd = $("#newPwd").val();
$(".error-message").text(""); // 기존 오류 메시지 초기화
if (!curPwd) {
$("#curPwdError").text("현재 암호를 입력하세요.");
return;
}
if (!newPwd) {
$("#newPwdError").text("새 암호를 입력하세요.");
return;
}
// 서버로 비밀번호 변경 요청 보내기
$.ajax({
type: "POST",
url: "changePwd.do",
data: { curPwd: curPwd, newPwd: newPwd },
success: function(response) {
if (response === "badCurPwd") {
$("#curPwdError").text("현재 암호가 일치하지 않습니다.");
} else {
alert("암호변경을 완료했습니다.");
// 성공적으로 변경되었으면 페이지 이동
window.location.href = "index.jsp";
}
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
alert("암호 변경 실패: " + status);
}
});
});
});
JSP
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>암호 변경</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Jua&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/css.css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="js/js.js"></script>
</head>
<body>
<div class="login-box">
<form action="changePwd.do" method="post">
<div class="user-box">
<p>
<label>현재 암호:</label><br/>
<input type="password" name="curPwd" id="curPwd">
<span id="curPwdError" class="error-message"></span>
</p>
</div>
<div class="user-box">
<p>
<label>새 암호:</label><br/><input type="password" name="newPwd" id="newPwd">
<span id="newPwdError" class="error-message"></span>
</p>
</div>
<div style="display: flex; justify-content: center;">
<button class="buttonS" id="changePw">암호변경
<span></span>
<span></span>
<span></span>
<span></span>
</button>
</div>
</form>
</div>
</body>
</html>
'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정' 카테고리의 다른 글
2023-08-22 66일차 (0) | 2023.08.23 |
---|---|
2023-08-21 65일차 (0) | 2023.08.21 |
2023-08-17 63일차 (0) | 2023.08.17 |
2023-08-16 62일차 (0) | 2023.08.16 |
2023-08-11 61일차 (0) | 2023.08.11 |