ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

์ค‘๋ณตํ™•์ธ์ด ํ•„์š”ํ•œ ํ•„๋“œ๋Š” ์ด๋ฉ”์ผ๊ณผ ์•„์ด๋””.

์ค‘๋ณตํ™•์ธ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ œ์ผ ํฌ๊ฒŒ๋Š” ์ค‘๋ณตํ™•์ธ์„ ๋ˆŒ๋Ÿฌ์„œ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๋‚˜๋Š” ์‹ค์‹œ๊ฐ„ ๋น„๋™๊ธฐ๋กœ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณตํ™•์ธ์„ ํ–ˆ๋‹ค.

mapper์—์„œ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” sql์„ ์ž‘์„ฑํ•˜์˜€๋‹ค. ๊ฐ€์ž…์ด ๋˜์–ด์žˆ๋Š” ํšŒ์›์ด๋ผ๋ฉด 1์„ ๋ฐ˜ํ™˜, ๊ฐ€์ž…์ด ๋˜์–ด์žˆ์ง€ ์•Š์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

 

Controller


UsersController

//์ด๋ฉ”์ผ ์ค‘๋ณตํ™•์ธ ์ฒดํฌ ์š”์ฒญ
@PostMapping("/emailCheck")
public Map<String, Object> confirmEmail(@RequestBody String userEmail) throws Exception {
	//ํด๋ผ์ด์–ธํŠธ์— ๋น„๋™๊ธฐ ํ†ต์‹ ์œผ๋กœ ๋ฐ›์•„์ค˜์•ผํ•˜๋‹ˆ๊นŒ @ReqeustBody๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
	
	//๊ฐ’ํ™•์ธ
	System.out.println("์ค‘๋ณต ํ™•์ธ ์š”์ฒญ๋œ ์ด๋ฉ”์ผ : " + userEmail);
	
	Map<String, Object> data = new HashMap<>();
	
	//์„œ๋น„์Šค ์ธก์— ์š”์ฒญ
	int result = usersService.isDuplicateEmail(userEmail);
	
	if(result == 0) {
		System.out.println("์ด๋ฉ”์ผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!");
		data.put("confirm", "OK");
	} else {
		System.out.println("์ด๋ฉ”์ผ ์ค‘๋ณต! ์‚ฌ์šฉ ๋ถˆ๊ฐ€!");
		data.put("confirm", "NO");
	}
	
	return data;
}

//์•„์ด๋”” ์ค‘๋ณตํ™•์ธ ์ฒดํฌ ์š”์ฒญ
@PostMapping("/idCheck")
public Map<String, Object> confirmId(@RequestBody String userId) throws Exception {
	//ํด๋ผ์ด์–ธํŠธ์— ๋น„๋™๊ธฐ ํ†ต์‹ ์œผ๋กœ ๋ฐ›์•„์ค˜์•ผํ•˜๋‹ˆ๊นŒ @ReqeustBody๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
	
	//๊ฐ’ํ™•์ธ
	System.out.println("์ค‘๋ณต ํ™•์ธ ์š”์ฒญ๋œ ์•„์ด๋”” : " + userId);
	
	Map<String, Object> data = new HashMap<>();
	
	//์„œ๋น„์Šค ์ธก์— ์š”์ฒญ
	int result = usersService.isDuplicateEmail(userId);
	
	if(result == 0) {
		System.out.println("์•„์ด๋”” ์‚ฌ์šฉ ๊ฐ€๋Šฅ!");
		data.put("confirm", "OK");
	} else {
		System.out.println("์•„์ด๋”” ์ค‘๋ณต! ์‚ฌ์šฉ ๋ถˆ๊ฐ€!");
		data.put("confirm", "NO");
	}
	
	return data;
}

์š”์ฒญ URI๋Š” ๊ฐ๊ฐ emailCheck, idCheck

๋น„๋™๊ธฐ ํ†ต์‹  json์œผ๋กœ ๋ณด๋‚ด๋ ค๋ฉด map์ด๋‚˜ ๊ฐ์ฒด๋กœ ๋ณด๋‚ด์•ผํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜๋ฅผ ๋ฐ›๋Š”๋ฐ ๋น„๋™๊ธฐ ํ†ต์‹ ์œผ๋กœ ๋ฐ›์•„์•ผํ•˜๋‹ˆ๊นŒ @RequestBody๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ€์ ธ์˜จ๋‹ค.

์„œ๋น„์Šค ์ธก์— ์š”์ฒญํ•ด์„œ result๊ฐ€ 0์ด๋ฉด ์‚ฌ์šฉ๊ฐ€๋Šฅ 1์ด๋ฉด ์‚ฌ์šฉ๋ถˆ๊ฐ€

 

 

Service


UsersService

//์ด๋ฉ”์ผ ์ค‘๋ณตํ™•์ธ ์ฒ˜๋ฆฌ
@Override
public int isDuplicateEmail(String email) throws Exception {
	return usersDAO.isDuplicateEmail(email);
}
//์•„์ด๋”” ์ค‘๋ณตํ™•์ธ ์ฒ˜๋ฆฌ
@Override
	public int isDuplicateId(String id) throws Exception {
	return usersDAO.isDuplicateId(id);
}

controller์™€ dao ์—ฐ๊ฒฐํ•˜๋Š” service๋‹จ ์ž‘์„ฑ

 

 

View


join.jsp

//์ด๋ฉ”์ผ ์ž…๋ ฅ๊ฐ’ ๊ฒ€์ฆ
$('#user_email').on('keyup', function(){
	console.log($(this).val());
	
	//๊ณต๋ฐฑํ™•์ธ
	if($('#user_email').val() === ""){
		$('#email_msg').html('<i style="color: red" class="far fa-times-circle"></i>');
		chk1 = false;
	
	//์ด๋ฉ”์ผ ์œ ํšจ์„ฑ ๊ฒ€์ฆ
	} else if(!getMailCheck.test($('#user_email').val())){
		
		$('#email_msg').html('<i style="color: red" class="far fa-times-circle"></i>');
		chk1 = false;
		
	//์ด๋ฉ”์ผ ์ค‘๋ณตํ™•์ธ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ
	} else {
		const email = $('#user_email').val();
		$.ajax({
			type: "POST",
			url: "/myapp/user/emailCheck",
			headers: {
                "Content-Type": "application/json",
                "X-HTTP-Method-Override": "POST"
            },
            data: email,
            datatype: "json",
            success: function(data){
            	console.log(data);
            	if(data.confirm === "OK"){
            		$('#email_msg').html('<i style="color: #262626;" class="far fa-check-circle"></i>');
            		chk1 = true;
            	} else {
            		$('#email_msg').html('<i style="color: red" class="far fa-times-circle"></i>');
            		chk1 = false;
            	}
            },
            error: function(error){
            	console.log("error : " + error);
            }
		});
	}
});

ajax๋กœ ์ค‘๋ณตํ™•์ธ ์š”์ฒญ์„ ๋ณด๋‚ผ๊ฑด๋ฐ  ํ•ด๋‹น inputํƒœ๊ทธ์— ์ด๋ฒคํŠธ๋ฅผ ๊ฑธ์–ด๋†“๋Š”๋‹ค.

keyup -> ํ•œ๊ธ€์ž ์“ธ๋•Œ๋งˆ๋‹ค ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. console.log($(this).val()); ๋กœ ์ฝ˜์†”์—์„œ ๊ฐ’์ด ์ž˜ ์ฐํžˆ๋Š”์ง€ ํ™•์ธ

๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋Œ์•„์˜ค๋Š”๊ฒŒ map์ด๋‹ˆ๊นŒ JSON์œผ๋กœ ์ž‘์„ฑํ•ด์ค€๋‹ค. data.confirm์—์„œ ํ™•์ธ ํ›„ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์ด๋ฉ”์ผ์˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์ฒ˜๋ฆฌ ํ•ด์ค€๋‹ค.

์•„์ด๋””๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ž‘์„ฑ ํ•ด ์ฃผ์—ˆ๋‹ค.

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€