프로그래밍/Java_Spring 게시판 만들기

Spring 게시판 만들기 (4) - 회원가입 기능 구현

개발계발게발 2021. 9. 3. 17:04
반응형

Ajax로 아이디 중복 처리

 

join.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입</title>
<style type="text/css">
</style>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.6.0.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		//alert("정상작동");
		$("#joinSubmit").click(function(){
			var id = $("#sm_id").val();
			var name = $("#sm_name").val();
			var pw1 = $("#sm_pw").val();
			var pw2 = $("#sm_pwconfirm").val();
			var email = $("#sm_email").val();
			var birthdate = $("#sm_birthdate").val();
			if(id == "" || id.length < 5 ){
				alert("올바른 ID를 입력해주세요.");
				$("#sm_id").focus();
				return false;
			} else if(name = "" || name.length < 5){
				alert("올바른 닉네임을 입력해주세요.")
				$("#sm_name").focus();
				return false;				
			}
			
			if(pw1 == "" || pw1.length < 6){
				alert("비밀번호를 6자 이상 입력해주세요.")
				$("#sm_pw").focus();
				return false;
			}			
			if(pw1.length > 5){
				if(pw1 == pw2 && pw2 == pw1){
					
				}else{
					alert("비밀번호가 일치하지 않습니다.")
					$("#sm_pw").focus();
					$("#sm_pw"="");
					$("#sm_pwconfirm"="");
					return false;				
				}
			} else if(email = "" || email.length < 5){
				alert("올바른 이메일을 입력해주세요.")
				$("#sm_email").focus();
				return false;
			} else if(birthdate = ""){
				alert("올바른 생일을 입력해주세요.")
				$("#sm_birthdate").focus();
				return false;
			}
		})	
	});
	function check(){
		//alert('변경되었 습니다.');		
		//alert($("#sm_id").val());
		var id = $("#sm_id").val();
		if(id == "" || id.length < 5){
			//alert("다섯 글자 이상 입력 해주세요.")
			$("#checkResult").css("color","red");
			$("#checkResult").text("아이디를 다섯 글자 이상 입력해주세요.");
			$("#sm_id").focus();
			$("#joinSubmit").attr("disabled",true);
			return false;
		}else{
			//alert("가입 가능 합니다.")
			$.ajax({
				url : "./checkID",
				type : "POST",
				cache : false,
				dataType : "html",
				data : {"sm_id" : id},
				success : function(data){					
					if(data == 0){
						$("#checkResult").css("color","blue");
						$("#checkResult").text("가입 가능합니다.");
						$("#joinSubmit").attr("disabled",false);
					}
					else{
						$("#checkResult").css("color","red");
						$("#checkResult").text("이미 등록된 아이디 입니다.");
						$("#joinSubmit").attr("disabled",true);
					}
				},
				error : function(request, status, error){
					alert(error);
				}
			})
			

		}
		
	}
</script>
</head>
<body>
	<div id="joinBox" class="modalBox">
		<h2 align="center">회원가입</h2>

		<form action="./join" method="post">
			<div>
				<input type="text" name="sm_id" id="sm_id" class="join_input"
					placeholder="아이디" required="required" onchange="check()">
				<p id="checkResult">아이디를 입력하세요.</p>
			</div>
			<div>
				<input type="text" name="sm_name" id="sm_name" class="join_input"
					placeholder="닉네임" required="required">
			</div>
			<div>
				<input type="password" name="sm_pw" id="sm_pw" class="join_input"
					placeholder="비밀번호" required="required">
			</div>
			<div>
				<input type="password" name="sm_pwconfirm" id="sm_pwconfirm"
					class="join_input" placeholder="비밀번호 확인" required="required">
			</div>
			<div>
				<input type="email" name="sm_email" id="sm_email" class="join_input"
					placeholder="이메일" required="required">
			</div>

			<div>
				<input type="date" name="sm_birthdate" id="sm_birthdate"
					class="join_input" placeholder="생일" required="required">
			</div>

			<input type="submit" id="joinSubmit" name="joinSubmit"
				disabled="disabled" value="가입하기">
		</form>

	</div>
</body>
</html>

 

LoginController.java 수정 

join 및 아이디 중복 처리기능 추가

 

package com.knowhoon.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.knowhoon.web.login.LoginDTO;
import com.knowhoon.web.login.LoginService;

@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	
	@GetMapping("/login")
	public String login() {
		
		return "login";
	}
	@PostMapping("/login")
	public String loginAction(LoginDTO loginDTO, HttpServletRequest request) {
		LoginDTO result = loginService.loginAction(loginDTO);
		System.out.println(request.getParameter("sm_id"));
		System.out.println(request.getParameter("sm_pw"));
		if(result != null) {
			request.getSession().setAttribute("sm_id", result.getSm_id());
			request.getSession().setAttribute("sm_name", result.getSm_name());
			request.getSession().setAttribute("sm_grade", result.getSm_grade());
			return "redirect:/board";			
		} else {
			if(request.getSession().getAttribute("sm_id") != null) {
				request.getSession().removeAttribute("sm_id");				
			}
			if(request.getSession().getAttribute("sm_name") != null) {
				request.getSession().removeAttribute("sm_name");							
			}
			if(request.getSession().getAttribute("sm_grade") != null) {
				request.getSession().removeAttribute("sm_grade");							
			}
			return "redirect:/login?error=loginError";
		}
		
	}
	@GetMapping("/logout")
	public String logout(HttpServletRequest request) {
		if(request.getSession().getAttribute("sm_id") != null) {
			request.getSession().removeAttribute("sm_id");
		}
		if(request.getSession().getAttribute("sm_name") != null) {
			request.getSession().removeAttribute("sm_name");							
		}
		if(request.getSession().getAttribute("sm_grade") != null) {
			request.getSession().removeAttribute("sm_grade");							
		}
		
		return "redirect:/board";
	}
	@GetMapping("/join")
	public String join() {		
		return "join";
	}
	@PostMapping("/join")
	public String joinAction(LoginDTO loginDTO) {
		int result = 0;
		result = loginService.joinAction(loginDTO);
		if(result == 1) {
			return "redirect:/board";			
		}else {
			return "redirect:/join?error=joinError";
		}
		
	}
	@PostMapping("/checkID")
	public @ResponseBody String checkID(HttpServletRequest request) {
		String check = "1";		
		check = loginService.checkID(request.getParameter("sm_id"));		
		return check;
	}
}

 

 

LoginService.java

package com.knowhoon.web.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
	@Autowired
	private LoginDAO loginDAO;
	
	public LoginDTO loginAction(LoginDTO loginDTO) {
		
		return loginDAO.loginAction(loginDTO);
	}

	public String checkID(String id) {
		return loginDAO.checkID(id);
	}

	public int joinAction(LoginDTO loginDTO) {
		return loginDAO.joinAction(loginDTO);
	}
}

 

 

LoginDAO.java

package com.knowhoon.web.login;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class LoginDAO {
	@Autowired
	private SqlSession sqlSession;
	
	public LoginDTO loginAction(LoginDTO loginDTO) {
		return sqlSession.selectOne("login.loginAction", loginDTO);
	}

	public String checkID(String id) {
		return sqlSession.selectOne("login.checkID", id);
	}

	public int joinAction(LoginDTO loginDTO) {
		return sqlSession.insert("login.joinAction", loginDTO);
	}	
}

 

 

LoginMapper.xml

joinAction, checkID 추가

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="login">		
	<select id="loginAction" parameterType="LoginDTO" resultType="LoginDTO" >
		SELECT sm_no, sm_name, sm_id, sm_grade FROM sloginview WHERE sm_id=#{sm_id} AND sm_pw=HEX(AES_ENCRYPT('knowhoon',SHA2(#{sm_pw},512))) 
	</select>
	<select id="checkID" parameterType="String" resultType="String">
		SELECT COUNT(1) FROM smember WHERE sm_id=#{sm_id}
	</select>
	<insert id="joinAction" parameterType="LoginDTO">
		INSERT INTO smember (sm_id, sm_pw, sm_name, sm_email, sm_birthdate) VALUES (#{sm_id}, HEX(AES_ENCRYPT('knowhoon',SHA2(#{sm_pw},512))), #{sm_name}, #{sm_email}, #{sm_birthdate} )
	</insert>
</mapper>

 

 

반응형