관리 메뉴

거니의 velog

231216_SPRING CRUD 보강 3 본문

대덕인재개발원_웹기반 애플리케이션

231216_SPRING CRUD 보강 3

Unlimited00 2023. 12. 16. 10:32

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<div class="page-header align-items-start min-vh-100"
	style="background-image: url('https://images.unsplash.com/photo-1497294815431-9365093b7331?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1950&q=80');">
	<span class="mask bg-gradient-dark opacity-6"></span>
	<div class="container my-auto">
		<div class="row">
			<div class="col-lg-4 col-md-8 col-12 mx-auto">
				<div class="card z-index-0 fadeIn3 fadeInBottom">
					<div
						class="card-header p-0 position-relative mt-n4 mx-3 z-index-2">
						<div
							class="bg-gradient-primary shadow-primary border-radius-lg py-3 pe-1">
							<h4 class="text-white font-weight-bolder text-center mt-2 mb-0">로그인</h4>
						</div>
					</div>
					<div class="card-body">
						<form role="form" class="text-start" method="post" action="/loginCheck.do" id="loginForm">
							<font class="text-primary font-weight-bold text-xs mt-1 mb-0 error">${errors.memId }</font>
							<div class="input-group input-group-outline my-3">
								<input type="text" class="form-control" id="memId" name="memId" placeholder="아이디">
							</div>
							<font class="text-primary font-weight-bold text-xs mt-1 mb-0 error">${errors.memPw }</font>
							<div class="input-group input-group-outline mb-3">
								<input type="password" class="form-control" id="memPw" name="memPw" placeholder="비밀번호">
							</div>
							<div class="text-center">
								<button type="button" class="btn bg-gradient-primary w-100 my-4 mb-2" id="loginBtn">로그인</button>
							</div>
							<p class="mt-4 text-sm text-center">
								아직 회원이 아닌가요? 
								<a href="/signup.do" class="text-primary text-gradient font-weight-bold">회원가입</a>
							</p>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
	$(function(){
		var loginBtn = $("#loginBtn");
		var loginForm = $("#loginForm");
		
		loginBtn.on("click", function(){
			var id = $("#memId").val();
			var pw = $("#memPw").val();
			
			if(!id) {
				alert("아이디를 입력해 주세요.");
				return false;
			}
			if(!pw) {
				alert("비밀번호를 입력해 주세요.");
				return false;
			}
			
			loginForm.submit();
		});
	});
</script>
package kr.or.ddit.controller.conn;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sun.java.swing.plaf.motif.resources.motif;

import kr.or.ddit.ServiceResult;
import kr.or.ddit.service.ILoginService;
import kr.or.ddit.vo.MemberVO;

@Controller
public class LoginController {
	
	@Inject
	private ILoginService loginService;
	
	@RequestMapping(value="/signin.do", method = RequestMethod.GET)
	public String signIn() {
		return "conn/signin";
	}
	
	@RequestMapping(value="/loginCheck.do", method = RequestMethod.POST)
	public String loginCheck(
			HttpSession session,
			MemberVO memberVO,
			Model model,
			RedirectAttributes ra
			) {
		String goPage = "";
		
		Map<String, String> errors = new HashMap<String, String>();
		if(StringUtils.isBlank(memberVO.getMemId())) {
			errors.put("memId", "아이디를 입력해주세요");
		}
		if(StringUtils.isBlank(memberVO.getMemPw())) {
			errors.put("memPw", "비밀번호를 입력해주세요");
		}
		
		if(errors.size() > 0) { // 에러가 있음
			model.addAttribute("errors", errors);
			model.addAttribute("memberVO", memberVO);
			goPage = "conn/signin";
		}else {
			MemberVO member = loginService.loginCheck(memberVO);
			if(member != null) {
				session.setAttribute("sessionInfo", member);
				ra.addFlashAttribute("message", member.getMemName() + "님, 환영합니다!");
				goPage = "redirect:/board/list.do";
			}else {
				model.addAttribute("message", "서버에러, 다시 시도해 주세요.");
				model.addAttribute("MemberVO", memberVO);
				goPage = "conn/signin";
			}
		}
		
		return goPage;
	}
	
	@RequestMapping(value="/signup.do", method = RequestMethod.GET)
	public String signUpForm() {
		return "conn/signup";
	}
	
	@RequestMapping(value = "/signup.do", method = RequestMethod.POST)
	public String signup(MemberVO member, Model model, RedirectAttributes ra) {
		String goPage = "";
		
		Map<String, String> errors = new HashMap<String, String>();
		if(StringUtils.isBlank(member.getMemId())) {
			errors.put("memId", "아이디를 입력해주세요");
		}
		if(StringUtils.isBlank(member.getMemPw())) {
			errors.put("memPw", "비밀번호를 입력해주세요");
		}
		if(StringUtils.isBlank(member.getMemName())) {
			errors.put("memName", "이름을 입력해주세요");
		}
		if(StringUtils.isBlank(member.getMemNickname())) {
			errors.put("memNickname", "닉네임을 입력해주세요");
		}
		
		if(errors.size() > 0) { // 에러가 있음
			model.addAttribute("errors", errors);
			model.addAttribute("MemberVO", member);
			goPage = "conn/signup"; // tiles의 definition으로 설정되어 있는 name 속성의 값과 매핑된다.
		}else {
			ServiceResult result = loginService.signup(member);
			if(result.equals(ServiceResult.OK)) { // 등록 성공
				ra.addFlashAttribute("message", "회원가입이 완료되었습니다.");
				goPage = "redirect:/signin.do";
			}else { // 등록 실패
				model.addAttribute("message", "서버에러, 다시 시도해 주세요.");
				model.addAttribute("MemberVO", member);
				goPage = "conn/signup";
			}
		}
		
		return goPage;
	}
	
	@RequestMapping(value = "/idCheck.do", method = RequestMethod.POST, produces = "text/plain; charset=utf-8")
	public ResponseEntity<String> idCheck(@RequestBody Map<String, String> map){
		ServiceResult result = loginService.idCheck(map.get("memId"));
		return new ResponseEntity<String>(result.toString(), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/nickNameCheck.do", method = RequestMethod.POST, produces = "text/plain; charset=utf-8")
	public ResponseEntity<String> nickNameCheck(@RequestBody Map<String, String> map){
		ServiceResult result = loginService.nickNameCheck(map.get("memNickname"));
		return new ResponseEntity<String>(result.toString(), HttpStatus.OK);
	}
	
}
package kr.or.ddit.service;

import kr.or.ddit.ServiceResult;
import kr.or.ddit.vo.MemberVO;

public interface ILoginService {

	public ServiceResult idCheck(String memId);
	public ServiceResult nickNameCheck(String memNickname);
	public ServiceResult signup(MemberVO member);
	public MemberVO loginCheck(MemberVO memberVO);

}
package kr.or.ddit.service.impl;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import kr.or.ddit.ServiceResult;
import kr.or.ddit.mapper.ILoginMapper;
import kr.or.ddit.service.ILoginService;
import kr.or.ddit.vo.MemberVO;

@Service
public class LoginServiceImpl implements ILoginService {

	@Inject
	private ILoginMapper loginMapper;
	
	@Override
	public ServiceResult idCheck(String memId) {
		ServiceResult result = null;
		
		MemberVO memberVO = loginMapper.idCheck(memId);
		if(memberVO != null) {
			result = ServiceResult.EXIST;
		}else {
			result = ServiceResult.NOTEXIST;
		}
		
		return result;
	}

	@Override
	public ServiceResult nickNameCheck(String memNickname) {
		ServiceResult result = null;
		
		MemberVO memberVO = loginMapper.nickNameCheck(memNickname);
		if(memberVO != null) {
			result = ServiceResult.EXIST;
		}else {
			result = ServiceResult.NOTEXIST;
		}
		
		return result;
	}

	@Override
	public ServiceResult signup(MemberVO member) {
		ServiceResult result = null;
		
		int status = loginMapper.signup(member);
		if(status > 0) {
			result = ServiceResult.OK;
		}else {
			result = ServiceResult.FAILED;
		}
		
		return result;
	}

	@Override
	public MemberVO loginCheck(MemberVO memberVO) {
		return loginMapper.loginCheck(memberVO);
	}

}
package kr.or.ddit.mapper;

import kr.or.ddit.vo.MemberVO;

public interface ILoginMapper {

	public MemberVO idCheck(String memId);
	public MemberVO nickNameCheck(String memNickname);
	public int signup(MemberVO member);
	public MemberVO loginCheck(MemberVO memberVO);

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="kr.or.ddit.mapper.ILoginMapper">

	<select id="idCheck" parameterType="string" resultType="memberVO">
		select 
			mem_no, mem_id, mem_pw, mem_name, mem_nickname, mem_regdate 
		from member 
		where mem_id = #{memId}
	</select>
	
	<select id="nickNameCheck" parameterType="string" resultType="memberVO">
		select 
			mem_no, mem_id, mem_pw, mem_name, mem_nickname, mem_regdate 
		from member 
		where mem_nickname = #{memNickname}
	</select>
	
	<insert id="signup" parameterType="memberVO">
		insert into member (
			mem_no, mem_id, mem_pw, mem_name, mem_nickname, mem_regdate 
		) values (
			seq_member.nextval, #{memId}, #{memPw}, #{memName}, #{memNickname}, sysdate
		)
	</insert>
	
	<select id="loginCheck" parameterType="memberVO" resultType="memberVO">
		select 
			mem_no, mem_id, mem_pw, mem_name, mem_nickname, mem_regdate 
		from member 
		where mem_id = #{memId} 
		and mem_pw = #{memPw}
	</select>

</mapper>


 

'대덕인재개발원_웹기반 애플리케이션' 카테고리의 다른 글

231216_SPRING CRUD 보강 5  (0) 2023.12.16
231216_SPRING CRUD 보강 4  (0) 2023.12.16
231216_SPRING CRUD 보강 2  (0) 2023.12.15
231216_SPRING CRUD 보강 1  (0) 2023.12.15
231213_SPRING 2 (16-1)  (0) 2023.12.12