Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[71] 230410 ๋™๊ธฐ์‹, ๋น„๋™๊ธฐ์‹, AJAX, Spring: ๊ฒŒ์‹œํŒ ๋Œ“๊ธ€ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 71์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/JavaScript, jQuery, Ajax, JSON

[71] 230410 ๋™๊ธฐ์‹, ๋น„๋™๊ธฐ์‹, AJAX, Spring: ๊ฒŒ์‹œํŒ ๋Œ“๊ธ€ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 71์ผ]

yjyuwisely 2023. 4. 10. 09:50

2304010 Mon 71st class

์ฐธ๊ณ : ๋™๊ธฐ์‹ vs ๋น„๋™๊ธฐ์‹
https://dev-coco.tistory.com/46
https://sudo-minz.tistory.com/21

์ฐธ๊ณ : ajax
https://azderica.github.io/00-javascript-ajax/


์›น ๋ธŒ๋ผ์šฐ์ € controller .jsp(ํ•ด๋‹น ํ™”๋ฉด์œผ๋กœ ์ด๋™)

๋™๊ธฐ์‹ ํ†ต์‹ ๋ฐฉ์‹

: ๊ธฐ์Šน์ „๊ฒฐ์ด ์žˆ๋‹ค. (์ˆœ์„œ) 
ex) ์นดํŽ˜: ์ฃผ๋ฌธํ•œ ํ›„์— ์ปคํ”ผ๊ฐ€ ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”

ํšŒ์›๊ฐ€์ž…
1)
์›น๋ธŒ๋ผ์šฐ์ € MemberController(controller)  Member.jsp(ํšŒ์›๊ฐ€์ž… ํ™”๋ฉด์œผ๋กœ ์ด๋™)

ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์„ ํด๋ฆญ
2)
์›น๋ธŒ๋ผ์šฐ์ € controller model(ํšŒ์›๊ฐ€์ž… ์ •๋ณด ์ˆ˜์ง‘) service <=> mapper(dao) <=> db
์›น๋ธŒ๋ผ์šฐ์ € <=> MemberController(controller) <=> MemberVO(model)(ํšŒ์›๊ฐ€์ž… ์ •๋ณด ์ˆ˜์ง‘) MemberService(service) <=> MemberMapper(mapper(dao)) Member Table insert <=> db

  MemberVO
(model)
(ํšŒ์›๊ฐ€์ž… ์ •๋ณด ์ˆ˜์ง‘)
  MemberVO
(model)
  MemberVO
(model)
   MemberVO
(model)
 
์›น๋ธŒ๋ผ์šฐ์ €  -----------> MemberController
(controller)
-----------> MemberService
(service)
-----------> MemberMapper
(mapper(dao)) 
-----------> Member Table insert


In software, a data access object (DAO) is a pattern that provides an abstract interface to some type of database or other persistence mechanism.

In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.


๋กœ๊ทธ์ธ
1)
์›น๋ธŒ๋ผ์šฐ์ € MemberController(controller)  Login.jsp(๋กœ๊ทธ์ธ ํ™”๋ฉด์œผ๋กœ ์ด๋™)

๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ํด๋ฆญ
2)
์›น๋ธŒ๋ผ์šฐ์ € controller model(ํšŒ์›๊ฐ€์ž… ์ •๋ณด ์ˆ˜์ง‘) service <=> mapper(dao) <=> db

์›น๋ธŒ๋ผ์šฐ์ € <=> MemberController(controller) <=> MemberVO(model)(ํšŒ์›๊ฐ€์ž… ์ •๋ณด ์ˆ˜์ง‘) MemberService(service) <=> MemberMapper(mapper(dao)) Member Table insert <=> db

  MemberVO
(model)
(ํšŒ์›๊ฐ€์ž… ์ •๋ณด ์ˆ˜์ง‘)
  MemberVO
(model)
  MemberVO
(model)
   MemberVO
(model)
 
์›น๋ธŒ๋ผ์šฐ์ €  <=====>  MemberController
(controller)
<===== MemberService
(service)
<===== MemberMapper
(mapper(dao)) 
<===== Member Table

๋น„๋™๊ธฐ์‹ ํ†ต์‹ ๋ฐฉ์‹ (ajax)

: ex) ์นดํŽ˜: ์ฃผ๋ฌธํ•œ ํ›„์— ์ง„๋™๋ฒจ์ด ์šธ๋ฆฌ๋ฉด ์ปคํ”ผ๋ฅผ ๊ฐ€์ง€๋Ÿฌ ์˜ค์„ธ์š”!

Ajax is a set of web development techniques that uses various web technologies on the client-side to create asynchronous web applications. With Ajax, web applications can send and retrieve data from a server asynchronously without interfering with the display and behaviour of the existing page.

.jsp์— ๋ณด๋‚ด๋Š” ๋™๊ธฐ์‹๊ณผ๋Š” ๋‹ฌ๋ฆฌ,
๋น„๋™๊ธฐ์‹์€ ์›น ๋ธŒ๋ผ์šฐ์ €ํ•œํ…Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋‹ค. 

AJAX ๋ฌธ๋ฒ• ๋ฐ ์†์„ฑ


JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays. It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers.


Spring Framework์˜ ๊ธฐ๋ณธ์ ์ธ ์–ธ์–ด๋Š” ์ž๋ฐ”์ด๋‹ค.
ํ•˜์ง€๋งŒ ๋น„๋™๊ธฐ์‹์€ ๋ฐ์ดํ„ฐํƒ€์ž…์ด javascript ๋˜๋Š” xml์ด ์ฃผ๋กœ ์–ธ์–ด์ด๋‹ค.

Spring Framework์—์„œ javascript ํƒ€์ž… ๋˜๋Š” xml์— ๋Œ€ํ•œ ์ธ์‹์ด pom.xml์—์„œ ํ•„์š”ํ•˜๋‹ค.

javascript ํƒ€์ž… ๋˜๋Š” xml์— ๋Œ€ํ•œ ์ธ์‹์— ํ•„์š”ํ•œ jar ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค.

the simplest way to parse JSON with Jackson.


jackson-databind(javascriptํƒ€์ž…)
jackson-dataformat-xml(xmlํƒ€์ž…)์„ pom.xml์— ์ถ”๊ฐ€ํ•œ๋‹ค.

		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
		    <groupId>com.fasterxml.jackson.core</groupId>
		    <artifactId>jackson-databind</artifactId>
		    <version>2.9.6</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
		<dependency>
		    <groupId>com.fasterxml.jackson.dataformat</groupId>
		    <artifactId>jackson-dataformat-xml</artifactId>
		    <version>2.9.6</version>
		</dependency>

๊ฒŒ์‹œํŒ์˜ ๋Œ“๊ธ€์€ ๋น„๋™๊ธฐ์‹์œผ๋กœ ๋งŒ๋“ ๋‹ค.
AJAX๋Š” jQuery์˜ ์ผ์ข…์ด๋‹ค.


230404 ์ž๋ฃŒ: SpringExํ…Œ์ด๋ธ” ๊ตฌ์กฐ

reply from reply.js and ReplyVO from ReplyController.java


detail.jsp

	<div id="chat">
		<ul id="replyUL">
		</ul>
	</div>

reply.js

function list(param){// listํ•จ์ˆ˜ ์„ ์–ธ ์‹œ์ž‘
	//alert(bno);
	var bno = param.bno;
	var page = param.page;
	
	console.log(bno)
	console.log(page)
	
	$.getJSON("/replies/"+bno+"/"+page+".json",function(data){
		console.log(data.replycnt)
		console.log(data.list)
		
		var str="";
		
		for(var i=0;i<data.list.length;i++){
			str+="<li>"+data.list[i].id+"</li>"
			str+="<li><textarea id='replycontent"+data.list[i].rno+"'>"+data.list[i].reply+"</textarea></li>"
			str+="<li>"
			str+="<input class='update' type='button' value='์ˆ˜์ •' data-rno="+data.list[i].rno+">"
			str+="<input class='remove' type='button' value='์‚ญ์ œ' data-rno="+data.list[i].rno+">"
			str+="</li>"
		}
		
		$("#replyUL").html(str);
		
		showReplyPage(data.replycnt,page);
	});
}// listํ•จ์ˆ˜ ์„ ์–ธ ๋


์†Œ์Šค๋ฅผ ๋ถ„์„ํ•œ๋‹ค. ์†Œ์Šค๋ฅผ ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์ด ๋จผ์ €๋‹ค. MVC, DB์— ์–ด๋–ป๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋˜์ง€๊ณ  ์žˆ๋Š”์ง€

detail.jsp

	<div><label>๋Œ“๊ธ€</label></div>
	<div>
		<textarea rows="5" cols="50" id="reply"></textarea>
	</div>
	<div>

reply.js

	// ๋Œ“๊ธ€ ์“ฐ๊ธฐ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด 
	$("#add").on("click",function(){
		// ๋Œ“๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋‹น์‹œ์— ๋Œ“๊ธ€๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ฌ๋ ค๋ฉด $("#add").on("click",function(){ ์•„๋ž˜์— ์„ ์–ธ
		var replyValue=$("#reply").val();
		var idValue="abcd1";
		// ๋Œ“๊ธ€์“ฐ๊ธฐ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ ํ˜ธ์ถœ
		add({bno:bnoValue,reply:replyValue,id:idValue});
	})

์ œ์ผ ํ•˜๋‹จ์˜ ์ฝ”๋“œ

// ๋Œ“๊ธ€ ์“ฐ๊ธฐ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ ์„ ์–ธ
function add(reply){	// addํ•จ์ˆ˜ ์„ ์–ธ ์‹œ์ž‘
	console.log(reply);
	$.ajax({
		type:"post",	// method๋ฐฉ์‹(get, post, put, delete)
		url:"/replies/new",
		data:JSON.stringify(reply),
		// contentType : ajax -> controller๋กœ ๋ฐ์ดํ„ฐ ์ „์†ก ์‹œ ํƒ€์ž…
		// (contentType์„ ์ƒ๋žตํ•˜๋ฉด web Browerํ•œํ…Œ ์œ„์ž„)
		contentType:"application/json; charset=utf-8",
		success:function(result){
			if(result=="success"){
				alert("๋Œ“๊ธ€์“ฐ๊ธฐ ์„ฑ๊ณต")
				list(reply.bno)
			}
		}
	})
}	// addํ•จ์ˆ˜ ์„ ์–ธ ๋

728x90
๋ฐ˜์‘ํ˜•
Comments