๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[Spring] AJAX ๋น„๋™๊ธฐ ํ†ต์‹  - ์•„์ด๋”” ์ค‘๋ณต ์ฒดํฌ ๊ธฐ๋Šฅ ๊ตฌํ˜„ (2023.06.01) ๋ณธ๋ฌธ

full stack/Back-End

[Spring] AJAX ๋น„๋™๊ธฐ ํ†ต์‹  - ์•„์ด๋”” ์ค‘๋ณต ์ฒดํฌ ๊ธฐ๋Šฅ ๊ตฌํ˜„ (2023.06.01)

Shinjio 2023. 6. 1. 17:42

๐ŸŽˆ AJAX (Asynchronous JavaScript and XML)

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๊ฐ„ ๋น„๋™๊ธฐ์  ์ •๋ณด ๊ตํ™˜์ด ๊ฐ€๋Šฅํ•œ ํ†ต์‹  ๊ธฐ๋Šฅ 

** ํ™”๋ฉด ์ „ํ™˜ ์—†์ด

 

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ ํ•˜๋‚˜
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋™ํ•˜๊ณ  ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์›น ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•˜์ง€ ์•Š๊ณ  ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๊ณ  ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•
  • ํ™”๋ฉด ๊ฐฑ์‹ ์ด ์—†์–ด์„œ ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ ๋งค์šฐ ํŽธ๋ฆฌํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋Š๊ปด์ง€๊ฒŒ ํ•จ
  • ํ•˜์ง€๋งŒ, ๋™์ ์œผ๋กœ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š” ๋งŒํผ ๊ฐœ๋ฐœ์ž์˜ ๊ตฌํ˜„์€ ๋ณต์žกํ•ด์ง

 

๐Ÿ“– Ajax ์žฅ์ 

  • ํŽ˜์ด์ง€์˜ ๊ฐฑ์‹  ์—†์ด ์„œ๋ฒ„์™€ ๋น„๋™๊ธฐ ํ†ต์‹ ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์คŒ
  • ํ™”๋ฉด์ด ์ƒˆ๋กœ ๋กœ๋”ฉ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ฏ€๋กœ ์†๋„๋ฉด์—์„œ ์„ฑ๋Šฅ ๊ฐœ์„  (์›นํŽ˜์ด์ง€ ์†๋„ ํ–ฅ์ƒ)
  • ๋ณ„๋„์˜ ํ”Œ๋Ÿฌ๊ทธ์ธ์ด ํ•„์š”ํ•˜์ง€ ์•Š์Œ
  • Http ์ „์†ก ์ค‘์—๋„ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ์ƒํ˜ธ์ž‘์šฉ ํ•  ์ˆ˜ ์žˆ์Œ

 

๐Ÿ’ป Ajax ํ™œ์šฉํ•˜์—ฌ ์•„์ด๋”” ์ค‘๋ณต ์ฒดํฌ ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜๊ธฐ - spring  / jquery 

 

1. index.jsp - html ๋ถ€๋ถ„

email ์ค‘๋ณต์ฒดํฌ input ํƒœ๊ทธ์— onClick ์ด๋ฒคํŠธ ์ง€์ •

 

<li><h5>ํšŒ์›๊ฐ€์ž…</h5></li>
	<form action="member/join" method="post">
		<li><input type="text" name="email" placeholder="Email์„ ์ž…๋ ฅํ•˜์„ธ์š”" id="input" ></li>
		<li><input type="button" value="email ์ค‘๋ณต์ฒดํฌ" onClick="checkEmail()"></li>
		<li><span id="result"></span></li>
		<li><input type="password" name="pw" placeholder="PW๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”" ></li>
		<li><input type="text" name="tel" placeholder="์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”" ></li>
		<li><input type="text" name="address" placeholder="์ง‘์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”" ></li>
		<li><input type="submit" value="JoinUs" class="button fit"></li>
	</form>


2. index.jsp - script ๋ถ€๋ถ„

email ์ธํ’‹ ํƒœ๊ทธ์—์„œ ์ง€์ •ํ•œ ์•„์ด๋”” (input) ๋ณ€์ˆ˜๋กœ ์ €์žฅ

ajax๋กœ memberController์˜ emailCheck๋กœ ์š”์ฒญ > ๋ฐ˜ํ™˜๊ฐ’(res) ๋ฐ›์•„์„œ id๊ฐ€ result์ธ <span>์— ๊ฐ’ ๋„ฃ๊ธฐ

 

<script>
	function checkEmail(){
	//์ด๋ฉ”์ผ ๊ฐ€์ ธ์˜ค๊ธฐ
	//์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ผ๋ฉด document.getElementById("email").value
	//innerHtml : ํƒœ๊ทธ ์‚ฌ์ด์˜ html ์š”์†Œ ํฌํ•จํ•˜๋ฉฐ ๊ฐ€์ ธ์˜ค๊ธฐ >>> ์ œ์ด์ฟผ๋ฆฌ์—์„œ๋Š” html()
	//html() >> ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ / html("<h1>s</h1>") >> ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ
	//innerText : ํƒœ๊ทธ ์‚ฌ์ด์˜ text๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ >>> ์ œ์ด์ฟผ๋ฆฌ์—์„œ๋Š” text()
	//value : input ํƒœ๊ทธ์— ์ž‘์„ฑ๋œ ๊ฐ’(์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ(์„ ํƒํ•œ) ๊ฐ’) ๊ฐ€์ ธ์˜ค๊ธฐ >>> ์ œ์ด์ฟผ๋ฆฌ์—์„œ๋Š” val()
		let input = $("#input").val()
		console.log(input)
					
	//๋น„๋™๊ธฐํ†ต์‹  : json {"key" : "value"} ํ‚ค๊ฐ’์œผ๋กœ ๊ตฌ๋ณ„, xml<> ํƒœ๊ทธ๋กœ ๊ตฌ๋ณ„ >> ์ž์ฃผ ์“ฐ๋Š” ํ˜•์‹
		$.ajax({
		//์š”์ฒญ๊ฒฝ๋กœ
			url : "member/emailcheck",
		//http ์š”์ฒญ ๋ฉ”์„œ๋“œ (get๋ฐฉ์‹์ด ๊ธฐ๋ณธ์œผ๋กœ ์ƒ๋žต ๊ฐ€๋Šฅ)
			type : "get", 
			data : {'input' : input },
			success : function(res){
		//res : ์„œ๋ฒ„ ์‘๋‹ต๊ฐ’
			console.log(res)
			if(res=="success"){
				$("#result").text('์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฉ”์ผ') 
			}else{//fail
				$("#result").text('์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ด๋ฉ”์ผ') 
			}
		},
		error : function(){
			alert("ํ†ต์‹ ์‹คํŒจ")
		}
	})
}
</script>

 

3. MemberController - emailCheck

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ „์†กํ•œ input๊ฐ’(์ด๋ฉ”์ผ) ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์Œ

์‘๋‹ตํ•  ๋•Œ๋Š” ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜

	//๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ  data(model)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•จ
	//@Controller >> view๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
	//@Controller + @ResponseBody ์‚ฌ์šฉ >> model์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
	@RequestMapping(value="/member/emailcheck")
	public @ResponseBody String emailCheck(@RequestParam("input") String email) {
		System.out.println(email);
		
		int result = mapper.emailCheck(email);
		
		System.out.println(result);
		
		if(result>0) { //๊ฐ’์ด ์ด๋ฏธ ์กด์žฌ >> ์‚ฌ์šฉ๋ถˆ๊ฐ€๋Šฅํ•œ ์ด๋ฉ”์ผ
			System.out.println("ss");
			return "fail"; //์ผ๋ฐ˜๋ฌธ์ž์—ด
		}else { //๊ฐ’์ด ์—†์Œ >> ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ด๋ฉ”์ผ
			System.out.println("qq");
			return "success";
		}
	}

 

4. MemberMapper 

annotaion ๋ฐฉ์‹ ํ™œ์šฉํ•˜์—ฌ mybatis ์‚ฌ์šฉ

sql๋ฌธ ์ž‘์„ฑ์‹œ ์กฐํšŒํ•˜๋Š” ์ •๋ณด๋Š” count(*) - intํ˜•

>> ๋งŒ์•ฝ ๊ฐ™์€ ์•„์ด๋””๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋‹ค๋ฉด ์ˆซ์ž 1์„ ์—†๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ

 

	//์ด๋ฉ”์ผ ์ค‘๋ณต ์ฒดํฌ
	@Select("select count(*) email from webmember where email=#{email}")
	public int emailCheck(String email);