# 환경

- Spring 4.6.1.RELEASE

- mybatis

- 뷰 페이지 : jsp 

- tomcat v9.0

 

# 구현 방법 정리

1. login.jsp : <form>에서 로그인 ID, PW를 받아서 controller로 넘긴다.  

2. controller : ID, PW 확인하는 로직을 수행 후 리턴값 널 체크, 조건문으로 분기한다.

 - ID, PW가 틀릴 경우 : HttpSession에 원하는 에러메세지를 담아서 로그인 페이지로 리턴한다.

3. login.jsp : ID 또는 PW가 틀렸을 경우 에러메세지를 보여준다.

 

#간략한 중요 코드

1. login.jsp

<script>
function login_submit() {
	var user_id = document.frm.user_id.value.trim().toUpperCase();
	var password = document.frm.password.value.trim();
	var send_frm = document.frm;

	if (user_id == "") {
	alert("아이디를 입력하여 주시기 바랍니다.");
	document.frm.user_id.focus();
	return false;
	}

	if (password == "") {
		alert("비밀번호를 입력하여 주시기 바랍니다.");
		document.frm.password.focus();
	return false;
	}
	send_frm.action = "host_main"
	send_frm.method = "post";
	send_frm.submit();
}
</script>

<body>
<form name="frm">
ID : <input type="text" name="user_id" id="user_id">
PW : <input type="password" name="password" id="password">
<img src="web/images/login_btn.gif" onclick="login_submit()" style="cursor: pointer;">
</form>

<input type="hidden" id="loginCheck" name="loginCheck" value="${check}">

<script>
	$(document).ready(function() {
		var check = $('#loginCheck').val();
		if(check != ""){
			alert(check);
			$('#loginCheck').val() = "";
		}
	});
</script>
</body>

 

2. Controller

@Controller
public class HostController {
	@Autowired
	private HostService service;
    
 	// 관리자 로그인처리
	@RequestMapping("host_main")
	public String goHostMain(UserVO vo, Model model, HttpSession session, HttpServletRequest request) {
    UserVO uvo = service.loginCheck(vo);
		
		if(uvo == null) {
			session.setAttribute("check", "ID를 확인해주세요.");
			return "host/login";
		}
        
        if(vo.getUser_id().equals(uvo.getUser_id())) {
			// id OK
			if(vo.getPassword().equals(uvo.getPassword())) {
				// pw OK
                
            } else {
				// PW NO OK
				session.setAttribute("check", "PW를 확인해주세요.");
				return "host/login";
			}
		} else {
			// ID NOT OK
			session.setAttribute("check", "ID를 확인해주세요.");
			return "host/login";
		}
		return "main/main";
	}

 

+ Recent posts