지금까지 회원가입과 로그인 기능을 구현했는데요
이제 사용자의 비밀번호를 변경하는 기능을 구현해보도록 하겠습니다.
1. userMapper
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD 선언 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="userMapper">
<insert id="register">
insert into user(identification, password, name)
values( #{identification}, #{password}, #{name})
</insert>
<select id="login" resultType="UserVO">
select identification, name, password
from user
where identification = #{identification} and password = #{password}
</select>
<update id="modify">
update user
set password = #{password}
where identification = #{identification}
</update>
</mapper>
비밀번호를 변경하는 조건으로 아이디를 사용했습니다.
2. UserDAO 작성
package kr.co.web.persistence;
import kr.co.web.domain.UserVO;
public interface UserDAO {
public void register(UserVO vo) throws Exception;
public UserVO login(UserVO vo) throws Exception;
public void modify(UserVO vo) throws Exception;
}
package kr.co.web.persistence;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import kr.co.web.domain.UserVO;
@Repository
public class UserDAOImpl implements UserDAO{
@Inject
private SqlSession session;
private static final String NS = "userMapper";
private static final String REGISTER = NS + ".register";
private static final String LOGIN = NS + ".login";
private static final String MODIFY = NS + ".modify";
@Override
public void register(UserVO vo) throws Exception {
session.insert(REGISTER, vo);
}
@Override
public UserVO login(UserVO vo) throws Exception {
return session.selectOne(LOGIN, vo);
}
@Override
public void modify(UserVO vo) throws Exception {
session.update(MODIFY, vo);
}
}
3. SERVICE 작성
package kr.co.web.service;
import kr.co.web.domain.UserVO;
public interface UserService {
public void register(UserVO vo) throws Exception;
public UserVO login(UserVO vo) throws Exception;
public void modify(UserVO vo) throws Exception;
}
package kr.co.web.service;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import kr.co.web.domain.UserVO;
import kr.co.web.persistence.UserDAO;
@Service
public class UserServiceImpl implements UserService {
@Inject
private UserDAO user;
@Override
public void register(UserVO vo) throws Exception {
user.register(vo);
}
@Override
public UserVO login(UserVO vo) throws Exception {
return user.login(vo);
}
@Override
public void modify(UserVO vo) throws Exception {
user.modify(vo);
}
}
4. Controller 작성
package kr.co.web.controller;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import kr.co.web.domain.UserVO;
import kr.co.web.service.UserService;
@Controller
@RequestMapping("/user/*")
public class UserController {
@Inject
private UserService service;
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@RequestMapping(value = "/register", method = RequestMethod.GET)
public void registerGET() throws Exception {
logger.info("registerGET");
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registerPOST(UserVO vo, RedirectAttributes ra) throws Exception {
logger.info("registerPOST");
service.register(vo);
ra.addFlashAttribute("result", "registerOK");
return "redirect:/";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginPOST(UserVO vo, HttpServletRequest req, RedirectAttributes ra) throws Exception {
logger.info("post login");
// HttpServletRequest를 사용하면 값을 받아 올수 있다.
HttpSession session = req.getSession();
UserVO login = service.login(vo);
if(login == null) {
session.setAttribute("user", null);
ra.addFlashAttribute("result", "loginFalse");
} else {
session.setAttribute("user", login);
String id = req.getParameter("identification"); // 확인용
ra.addFlashAttribute("result", "loginOK");
}
return "redirect:/";
}
@RequestMapping(value ="/logout", method = RequestMethod.GET)
public String logout(HttpSession session) throws Exception {
logger.info("logout");
session.invalidate();
return "redirect:/";
}
@RequestMapping(value ="/modify", method = RequestMethod.GET)
public void modifyGET() throws Exception {
logger.info("modifyGET");
}
@RequestMapping(value ="/modify", method = RequestMethod.POST)
public String modifyPOST(HttpSession session, UserVO vo, RedirectAttributes ra) throws Exception {
logger.info("modifyPOST");
service.modify(vo);
session.invalidate();
ra.addFlashAttribute("result", "updateOK");
return "redirect:/";
}
}
5. modify.jsp 생성
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>게시판</title>
</head>
<body>
<form method="post" autocomplete="off">
<div>
<label for="identification">아이디</label>
<input type="text" id="identification" name="identification" value="${user.identification}" readonly/>
</div>
<div>
<label for="password">새 비밀번호</label>
<input type="password" id="password" name="password" />
</div>
<div>
<button type="submit">회원정보 수정</button>
</div>
</form>
<div>
<button onclick ="location.href='/'">처음</button>
</div>
</body>
</html>
뭔가 익숙하지 않나요?
회원가입할 때 작성했던 코드와 비슷한데요.
저희는 회원변경을 할것이기 때문에
아이디부분에 아까 세션 "user"로 보낸것을
아이디부분에 넣어주고 변경할 수 없도록 readonly를 적어주었습니다.
이제 home.jsp로 돌아가서 로그인을 했을 때 회원정보 수정이 있어야 하기에
추가해주도록 합니다.
<button type="button" onclick="location.href='/user/modify'">회원정보 수정</button>
비밀번호를 aaas 로 변경하겠습니다.
정상적으로 변경되었습니다.
'JAVA > blog' 카테고리의 다른 글
아이디 중복검사 기능 (0) | 2020.08.04 |
---|---|
회원(4) - 회원탈퇴 (0) | 2020.07.31 |
회원 (2) - 로그인, 로그아웃 기능 (0) | 2020.07.31 |
회원 (1) - 가입 기능 (0) | 2020.07.31 |
댓글 구현(3) - 게시판 댓글 수정, 삭제 (0) | 2020.07.29 |