Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

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

[62] 230328 Spring: ์Šคํ”„๋ง์— MVC ์ค‘ C(์ปจํŠธ๋กค๋Ÿฌ) ์‘์šฉํ•˜๊ธฐ, MVC ๊ตฌ์กฐ ์ดํ•ด [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 62์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[62] 230328 Spring: ์Šคํ”„๋ง์— MVC ์ค‘ C(์ปจํŠธ๋กค๋Ÿฌ) ์‘์šฉํ•˜๊ธฐ, MVC ๊ตฌ์กฐ ์ดํ•ด [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 62์ผ]

yjyuwisely 2023. 3. 28. 09:46

230328 Tue 62nd class

Ch. 18 MVC ํŒจํ„ด ๊ตฌํ˜„
์ง„๋„: Ch. 18 MVC ํŒจํ„ด ๊ตฌํ˜„์— ๊ธฐ๋ฐ˜ํ•œ ์ž์ฒด ์ˆ˜์—… (๊ต์žฌ: ์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€, ์ €์ž: ์ตœ๋ฒ”๊ท )
์ฑ… ์˜ˆ์ œ ์ฝ”๋“œ: https://github.com/madvirus/jsp23
์ €์ž ๋ธ”๋กœ๊ทธ: https://javacan.tistory.com/

 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | ์ตœ๋ฒ”๊ท  - ๊ต๋ณด๋ฌธ๊ณ 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | [์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€]๋Š” JSP 2.3์˜ ์ƒˆ๋กœ์šด ํŠน์ง• ๋ฐ˜์˜๊ณผ JSP๋ฅผ ์ง€์›ํ•˜๋Š” ์š”์†Œ์ธ ์„œ๋ธ”๋ฆฟ๊ณผ ํ‘œํ˜„ ์–ธ์–ด ๋“ฑ์˜ ์ƒˆ๋กœ์šด

product.kyobobook.co.kr


์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


http://localhost:8080/ -> Controller -> View(/WEB-INF/views/home.jsp)

public String home(				) {
		return "home"; // /WEB-INF/views/home.jsp
	}



http://localhost?id=aaaa&password=1234 --> Controller(Homecontroller) --> View(/WEB-INF/views/home.jsp)

public String home(String id, String password) {
		return "home"; // /WEB-INF/views/home.jsp
	}

์Šคํ”„๋ง์€ ์ž๋™ ์ˆ˜์ง‘ํ•ด์ค€๋‹ค. (์ž๋ฐ”๋Š” ๋‘ ๊ฐœ๋งŒ ์ผ์น˜์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.) 
home์€ ์ž๋ฐ”์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค. ๋ฉ”์„œ๋“œ๊ฐ€ ์ด๋Ÿฐ ์‹์œผ๋กœ ํ™œ์šฉ๋œ๋‹ค.


์ฝ”๋“œ) HomeController.java

package org.hj.controller;

import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
// ์ ‘๊ทผ์ œ์–ด์ž  ๋ฐ˜ํ™˜ํƒ€์ž…  ๋ฉ”์„œ๋“œ๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜)
//public ์ ‘๊ทผ ์ œํ•œ์ž, String ๋ฆฌํ„ด ํƒ€์ž…, home ๋ฉ”์„œ๋“œ, (๋งค๊ฐœ๋ณ€์ˆ˜ํƒ€์ž…,๋งค๊ฐœ๋ณ€์ˆ˜๋ช…)
	public String home(Locale locale, Model model) { //์ง€๊ธˆ์€ ์“ฐ์ž„์ด ์—†๋‹ค. (๊ทธ๋ƒฅ ์žˆ๋Š” ๊ฒƒ.)
		return "home"; //home์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. //๋ฉ”๋ชจ์žฅ์˜ URL์ฃผ์†Œ๊ฐ€ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด๋‹ค. 
						// /WEB-INF/views/home.jsp
	}
	@RequestMapping(value = "/drop", method = RequestMethod.GET)
	// ์ ‘๊ทผ์ œ์–ด์ž  ๋ฐ˜ํ™˜ํƒ€์ž…  ๋ฉ”์„œ๋“œ๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜)
		public String drop() {
			return "drop";
		}
}

์ˆœ์„œ๋Œ€๋กœ C(org.hj.controller), M(org.hj.model), V(views)์ด๋‹ค. 


http://localhost?id=aaaa&password=1234 --> Controller(Homecontroller) --> View(/WEB-INF/views/home.jsp)
(๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๋ฐฉ์‹)

public String home(String id, String password) {
		return "home"; // /WEB-INF/views/home.jsp
	}


http://localhost?id=aaaa&password=1234 --> Controller(Homecontroller) --> View(/WEB-INF/views/home.jsp)

public String home(LoginVO log) {
		return "home"; // /WEB-INF/views/home.jsp
	}

id๋Š” public void setId(String id) ์ฝ”๋“œ์—, password๋Š” public void setPassword(String password) ์ฝ”๋“œ์™€ ์—ฐ๊ฒฐ๋œ๋‹ค.  

ํฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” ์œ„ ๋ฐฉ์‹๋ณด๋‹ค๋Š” ์•„๋ž˜ ๋ฐฉ์‹์œผ๋กœ ํ•ด์•ผ ํšจ์œจ์ ์ด๊ณ , ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ์ด ๋œ๋‹ค. 
MVC ์ค‘์—์„œ Model์„ ์“ฐ๋Š” ์ด์œ ์ด๋‹ค. :)

์ฝ”๋“œ) src/main/java/org.hj.model LoginVO.java

public class LoginVO {


	private String id;
	private String password;
    
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

์›น ๋””์ž์ด๋„ˆ(V), ์›น ๊ฐœ๋ฐœ์ž(M, C)๊ฐ€ ๋™์‹œ์— ์ผํ•˜๊ฒŒ ํ•œ๋‹ค.
Updates: ์‚ฌ์šฉ์ž๊ฐ€ www.naver.com  URL ์“ด๋‹ค.
๋ชจ๋ธ์€ ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. (๋ฐ์ดํ„ฐ ์‹ค์„ ๊ฒŒ ์—†์œผ๋ฉด ๊ทธ๋ƒฅ ๊ฐ„๋‹ค.)


์ง€๊ธˆ ํŒŒ์ผ์—์„œ๋Š” 3.0์ด์ƒ์œผ๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์„œ ๋ณ€๊ฒฝํ•  ๊ฑด ์—†๋‹ค.
์ฝ”๋“œ) pom.xml ์ผ๋ถ€

		<!-- Servlet -->
		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>3.1.0</version>
		    <scope>provided</scope>
		</dependency>

์–ด์ œ ํ•œ ๋‚ด์šฉ์ธ๋ฐ ์ฒซ๋ฒˆ์งธ ๋ฐฉ์‹์„ ์„ ํƒํ–ˆ์—ˆ๊ณ , ๋‘˜ ์ค‘ ํ•˜๋‚˜์˜ ๋ฐฉ์‹์œผ๋กœ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ๋กœ๋ฅผ ๋ณ€๊ฒฝํ•œ๋ฉด ๋œ๋‹ค.


Spring MVC๋งŒ ์‹ ๊ฒฝ์“ด๋‹ค.

์ผ์ผ์ด ๋‹ค ์„ค์ •ํ•ด์•ผํ•˜๋Š” JSP์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์Šคํ”„๋ง์€ ํ™˜๊ฒฝ ์„ค์ •์ด ๋‹ค ๋งž์ถฐ์ ธ์žˆ๋‹ค. 
์šฐ๋ฆฌ๋Š” ๋‘๋ฒˆ์งธ ๋‹จ๊ณ„์˜ Spring MVC๋งŒ ์‹ ๊ฒฝ์“ฐ๋ฉด ๋œ๋‹ค. 


1. Request ์•ž์—๋Š” ์‚ฌ์šฉ์ž ํ™”๋ฉด์ด ์žˆ๋‹ค. ์‚ฌ์šฉ์ž ํ™”๋ฉด -> 1. request 
์ผ์ผ์ด JSP์˜ request.getParameter() ์•ˆ ํ•ด๋„ ๋œ๋‹ค. ์ฐธ๊ณ : https://chan-t-story.tistory.com/16
Request์ฒ˜๋ฆฌ ํ•œ ๊ฒŒ Controller ์•ˆ์— ๋“ค์–ด๊ฐ€์žˆ๋‹ค. 

์—ฌ๊ธฐ์„œ๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋งŒ ์ผ์น˜์‹œํ‚ค๋ฉด ๋œ๋‹ค. 

์–ด๋–ค ๋ชจ๋ธ์ธ์ง€ ๋งค๊ฐœ ๋ณ€์ˆ˜์— ์–ธ๊ธ‰ํ•˜๋ฉด ๋œ๋‹ค. (๋ณ€์ˆ˜ ์ด๋ฆ„ ๋งž์ถฐ์ค€๋‹ค.) 
๋ฐ์ดํ„ฐ ์‹ค์„ ๊ฒŒ ์—†์œผ๋ฉด ๋ฐ”๋กœ View๋กœ ๊ฐ€๋ฉด ๋œ๋‹ค. (๋ชจ๋ธ ์—†์–ด๋„ ๋œ๋‹ค.)


์Šคํ”„๋ง์—๋Š” ์ž๋™์œผ๋กœ ๋‹ค ๋˜์–ด์žˆ๋‹ค.


DI(Dependency Injection) - ์Šคํ”„๋ง์ด ๊ฐ์ฒด๋ฅผ ๋Œ€์‹  ์ƒ์„ฑํ•˜์—ฌ ์ฃผ์ž…ํ•œ๋‹ค.

@Autowired

๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์Šคํ”„๋ง์˜ ์• ๋„ˆํ…Œ์ด์…˜์ด๋‹ค. ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ์‹์—๋Š” @Autowired ์™ธ์— Setter ๋˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ์žˆ๋‹ค. ์ˆœํ™˜์ฐธ์กฐ ๋ฌธ์ œ์™€ ๊ฐ™์€ ์ด์œ ๋กœ @Autowired ๋ณด๋‹ค๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ์ฃผ์ž…๋ฐฉ์‹์ด ๊ถŒ์žฅ๋œ๋‹ค. ํ•˜์ง€๋งŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด์˜ ์ฃผ์ž…์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ์‹œ์—๋งŒ @Autowired๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ค์ œ ์ฝ”๋“œ ์ž‘์„ฑ์‹œ์—๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ๊ฐ์ฒด ์ฃผ์ž…๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค.

์ฐธ๊ณ  https://wikidocs.net/160890


์–ด๋…ธํ…Œ์ด์…˜(@, annotation)์ด๋ž€?

Annotation์€ Java5๋ถ€ํ„ฐ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ ๋ฌธ๋ฒ•์š”์†Œ์ด๋‹ค.
์‚ฌ์ „์ ์œผ๋กœ๋Š” "์ฃผ์„"์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ์ž๋ฐ” ์ฝ”๋“œ์— @๋ฅผ ์ด์šฉํ•ด ์ฃผ์„์ฒ˜๋Ÿผ ๋‹ฌ์•„ ํŠน์ˆ˜ํ•œ ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•ด์ค€๋‹ค.
ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹Œ ํ”„๋กœ๊ทธ๋žจ์— ๊ด€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์ฝ”๋“œ์— ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ •ํ˜•ํ™”๋œ ๋ฐฉ๋ฒ•์ด๋‹ค.
์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์ง€๊ณ  ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์ฐธ๊ณ 

์–ด๋…ธํ…Œ์ด์…˜์˜ ์˜ˆ์‹œ๊ฐ€ ์•„๋ž˜์— ์žˆ๋‹ค. ex) Controller, RequestMapping
์ฝ”๋“œ) HomeController.java

@Controller //Controller ์–ด๋…ธํ…Œ์ด์…˜
public class HomeController {
	
	@RequestMapping(value = "/", method = RequestMethod.GET) //RequestMapping ์–ด๋…ธํ…Œ์ด์…˜
	public String home(Locale locale, Model model) { 
		return "home"; //home์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. //๋ฉ”๋ชจ์žฅ์˜ URL์ฃผ์†Œ๊ฐ€ ํ˜ธ์ถœํ•œ ๊ฒƒ์ด๋‹ค. 
						// /WEB-INF/views/home.jsp
	}

์˜ˆ์‹œ) ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ž ์–ด๋–ค ํด๋ž˜์Šค์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
์ปจํŠธ๋กค๋Ÿฌ, ๋ชจ๋ธ ๋“ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ๊ฐ์ž์˜ ํด๋ž˜์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

package org.hj.controller;

@Controller
public class A { //ํด๋ž˜์Šค - controller ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) { 
		return "home";// /WEB-INF/views/home.jsp
	}
}    

@Service
public class B { //ํด๋ž˜์Šค - service ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค
	
    BoardMapper bm; //board ํ…Œ์ด๋ธ” mapper
    BoardAttachMapper bam; //attach ํ…Œ์ด๋ธ” mapper
    
	}

์ปจํŠธ๋กค๋Ÿฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 
์ฝ”๋“œ) servlet-context.xml ์ผ๋ถ€
<beans> ... </beans> ์•ˆ์— ๋“ฑ๋ก์„ ํ–ˆ๋‹ค.

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="org.hj.controller" />

</beans:beans>

ํ”„๋ก ํŠธ ์ชฝ์ด sevlet-context.xml
๋ฐฑ์—”๋“œ ์ชฝ์ด root-context.xml์ด๋‹ค.


RequestMapping์˜ ์˜ˆ์‹œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋‘ ๊ฐœ์˜ ๋ฐฉ์‹์€ ๊ฐ™๋‹ค. 

package org.hj.controller;

@Controller
@RequestMapping(value = "sample") // ์œ„ ์Šฌ๋ผ์ด๋“œ ์‡ผ์— ์žˆ๋Š” ๋ฐฉ์‹
public class A { //ํด๋ž˜์Šค - controller ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค
	@RequestMapping(value = "aaa", method = RequestMethod.GET)
	public String home(Locale locale, Model model) { 
		return "home";// /WEB-INF/views/home.jsp
	}
}    

@Controller
public class A { //ํด๋ž˜์Šค - controller ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค
	@RequestMapping(value = "sample/aaa", method = RequestMethod.GET)
	public String home(Locale locale, Model model) { 
		return "home";// /WEB-INF/views/home.jsp
	}
}

์ถ•์•ฝํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

package org.hj.controller;

@Controller
@RequestMapping(value = "sample") // ์œ„ ์Šฌ๋ผ์ด๋“œ ์‡ผ์— ์žˆ๋Š” ๋ฐฉ์‹
public class A { //ํด๋ž˜์Šค - controller ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค
    //@RequestMapping(value = "aaa", method = RequestMethod.GET)
    @GetMapping(value = "aaa")
    public String home(Locale locale, Model model) { 
        return "home";// /WEB-INF/views/home.jsp
    }
}    

public class A { //ํด๋ž˜์Šค - controller ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค
    //@RequestMapping(value = "aaa", method = RequestMethod.POST) ๋˜๋Š” ์•„๋ž˜ ๋ฐฉ์‹
    @POSTMapping(value = "aaa")
    public String home(Locale locale, Model model) { 
        return "home";// /WEB-INF/views/home.jsp
    }
}

ex01 ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋”ฐ๋กœ request.getParameter() ํ•  ํ•„์š”์—†์ด ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ณ  ๋ณ€ํ™˜ํ•œ๋‹ค.

์ฝ”๋“œ) LoginVO.java
์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผ์ด ์•ˆ ๋˜๋‹ˆ, ๊ฐ„์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฑธ ๋งŒ๋“ค์—ˆ๋‹ค.

package org.hj.model;

public class LoginVO {

	private String id;
	private String password;
	private String addr;
	private String phone;
	private String email;
	private String name;
	private int age;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "LoginVO [id=" + id + ", password=" + password + ", addr=" + addr + ", phone=" + phone + ", email="
				+ email + ", name=" + name + ", age=" + age + "]";
	}
}

@RequestParam("ids")๋Š” ๋นผ๋„ ๋œ๋‹ค.

์ž๋ฐ”์—๋Š” ๋‘ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๋ฐฐ์—ด์ด ์žˆ๋‹ค.
1. ๊ณ ์ • ๋ฐฐ์—ด

int[]score = new int[5];

๊ณ ์ • ๋ฐฐ์—ด์˜ ๋‹จ์ : ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„

2. ๊ฐ€๋ณ€ ๋ฐฐ์—ด

ArrayList<Integer> score = new ArrayList<Integer>

๊ฐ€๋ณ€ ๋ฐฐ์—ด์˜ ์žฅ์ : ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„ ๋ฐฉ์ง€, ๋ถ€์กฑํ•œ ์ˆ˜๋„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

Array: ์ž๋™ ์ •๋ ฌ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค. ๊ฒ€์ƒ‰์€ ๋Š๋ฆฌ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. 

Hashing map: ์ž๋™ ์ •๋ ฌ ๊ธฐ๋Šฅ์€ ์—†์ง€๋งŒ ๊ฐ ํ‚ค์— ๋Œ€ํ•œ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. 
์ธ๋ฑ์Šค๊ฐ€ ํ‚ค์ด๋ฏ€๋กœ (ex. a, b, c, d) ํ‚ค๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒ€์ƒ‰์ด ์šฉ์ดํ•˜๋‹ค.


Model model: ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ–ˆ๋‹ค.
model.addAttribute: ๋ชจ๋ธ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Model: ์ปจํŠธ๋กค๋Ÿฌ์— ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋ฅผ JSP๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด

Model: ์„œ๋ฒ„์—์„œ ํ™”๋ฉด์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. (ํ™”๋ฉด์— ๋ฟŒ๋ ค์ค€๋‹ค.)
"serverTime"์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ™์ด ์‹ค์–ด์„œ home.jsp๋กœ ์ด๋™ํ•œ๋‹ค. 
JSP์˜ ResultSet๊ณผ ๋น„์Šทํ•˜๋‹ค. 



๊ทธ๋ƒฅ int page๋ฉด ํ™”๋ฉด์œผ๋กœ ์•ˆ ๊ฐ„๋‹ค.
@ModelAttribute("page")๊ฐ€ ์•ž์— ์žˆ์œผ๋ฏ€๋กœ ํ™”๋ฉด์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.

SampleDTO์˜ ์ •์˜ (์•ž ์Šฌ๋ผ์ด๋“œ์—์„œ ๊ฐ€์ ธ์™”๋‹ค.)

${sampleDTO}, ${page}: sampeDTO, page๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. 

์–ด๋…ธํ…Œ์ด์…˜์ด๋ผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์œผ๋กœ ์ค€๋‹ค. 
localhost:8080/sample/ex04?name=aaa&age=11&page=9


RedirectAttribute: ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋˜ ๋‹ค๋ฅธ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.


- String
- void
- VO, DTO

- ResponseEntity ํƒ€์ž…
- Model, ModelAndView
- HttpHeaders

(๋ฉ”์„œ๋“œ์˜ ๊ตฌ์„ฑ: ๋ฆฌํ„ด ํƒ€์ž…, ๋ฉ”์„œ๋“œ ์ด๋ฆ„, ๋งค๊ฐœ ๋ณ€์ˆ˜)


Controller์˜ ๋ฆฌํ„ดํƒ€์ž…

- String ํƒ€์ž…

๋ฆฌํ„ด ํƒ€์ž…์ด String์ด๋ฉด ๋ฌธ์ž์—ด ๋’ค์— .jsp๋ฅผ ๋ถ™์ธ๋‹ค.

String ํƒ€์ž…์˜ ์˜ˆ์‹œ

sample/aaa ์ธ๋ฐ ์‹ค์ œ๋กœ ํ™”๋ฉด์— ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ bbb.jsp์ด๋‹ค. 

- ๊ธฐ๋ณธ: forward ๋ฐฉ์‹
.jsp๋ฅผ ๋ถ™์—ฌ์„œ ์‹คํ–‰ํ•œ๋‹ค.

	// ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
	public String list (Model model, CriteriaVO cri) {
		System.out.println(cri);
		// list.jsp ์‹คํ–‰ ํ•  ๋•Œ select ๋œ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์™€๋ผ
		model.addAttribute("list", bs.list(cri));
		// list.jsp ์‹คํ–‰ ํ•  ๋•Œ PageVO์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€๋ผ.
		//                           ์ƒ์„ฑ์ž ํ˜ธ์ถœ(๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ ์ƒ์„ฑ์ž)
		// boardํ…Œ์ด๋ธ”(๊ฒŒ์‹œํŒํ…Œ์ด๋ธ”)์— ์ „์ฒด ๊ฑด์ˆ˜(selectํ•ด์„œ)๋ฅผ ์•„๋ž˜์— 190๋Œ€์‹ ์— ๋Œ€์ž…
		int total=bs.total(cri);
		//model.addAttribute("paging", new PageVO(cri, 190));
		model.addAttribute("paging", new PageVO(cri, total));
		return "board/list";
	}
	// ๊ฒŒ์‹œํŒ ์ƒ์„ธ ํŽ˜์ด์ง€
	@RequestMapping(value = "/board/detail", method = RequestMethod.GET)
	// public String detail(int bno){
	public String detail (BoardVO board, Model model) {
		System.out.println(board);
		// bs.detail(bno);
		model.addAttribute("detail", bs.detail(board));
		return "board/detail";
	}

- redirect ๋ฐฉ์‹
redirect:/detail์— .jsp๋ฅผ ๋ถ™์ด์ง€ ์•Š๋Š”๋‹ค.
๋˜ ๋‹ค๋ฅธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค. (ํ™”๋ฉด์„ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.)

	// ๊ฒŒ์‹œํŒ ์ˆ˜์ •
	@RequestMapping(value = "/board/modify", method = RequestMethod.POST)
	public String modify (BoardVO board, RedirectAttributes rttr) {
		bs.modify(board);
		rttr.addAttribute("bno", board.getBno());
		// ์ˆ˜์ •ํ•˜๊ณ  ๋‚œ ๋’ค ํ™•์ธ์„ ์œ„ํ•ด ์ƒ์„ธํŽ˜์ด์ง€๋กœ ํ™”๋ฉด์ด๋™
		return "redirect:/detail";
	}
	
	// ๊ฒŒ์‹œํŒ ์‚ญ์ œ
	@RequestMapping(value = "/board/remove", method = RequestMethod.POST)
	public String remove (BoardVO board) {
		bs.remove(board);
		return "redirect:/list";
	}

 

๊ฒŒ์‹œํŒ ์ˆ˜์ •์˜ board:/detail์ด ๊ฒŒ์‹œํŒ ์ƒ์„ธ์˜ board/detail์„ ์‹คํ–‰์‹œํ‚จ๋‹ค.
๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•œ ๊ณณ์—๋‹ค๊ฐ€ "bno"๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ธ๋‹ค.

์•ž์˜ ์Šฌ๋ผ์ด๋“œ

๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ ๋ณด๋‚ด๊ณ  ์†Œ๋ฉธํ•œ๋‹ค. 

๊ฒŒ์‹œํŒ ์‚ญ์ œ์˜ redirect:/list๊ฐ€ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ์˜ board/list๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.


- void ํƒ€์ž…

Message๋ฅผ ๋ณด๋ฉด sample/ex05๊ฐ€ URL์ฃผ์†Œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ„๋‹ค. ๋ฆฌํ„ด์ด ์—†์œผ๋ฏ€๋กœ ๊ทธ๋ ‡๋‹ค.

๋ฆฌํ„ด ํƒ€์ž…์ด void์ผ ๊ฒฝ์šฐ localhost:8080/sample/ex05์—์„œ jsp ๋ถ™์—ฌ์„œ(sample/ex05.jsp) ์‹คํ–‰ํ•œ๋‹ค.
URL์ฃผ์†Œ์™€ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” .jsp๊ฐ€ ๋™์ผํ•˜๋ฉด ๊ตณ์ด ๋ฆฌํ„ด ํƒ€์ž…์€ ํ•„์š”์—†๋‹ค. (void๋กœ ๋‘๋ฉด ๋œ๋‹ค.)

๋™์ผํ•˜์ง€ ์•Š์œผ๋ฉด, ๋ฆฌํ„ด ํƒ€์ž…์„ ๋‘ฌ์•ผํ•œ๋‹ค. 

Void ํƒ€์ž…์˜ ์˜ˆ์‹œ


SampleDTO๋Š” ๋ชจ๋ธ ํƒ€์ž…์ด๋‹ค.
return dto: SampleDTO์— ์ €์žฅ๋œ ๊ฐ’์„ ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋ณด๋‚ธ๋‹ค. 

๊ฐ์ฒด ํƒ€์ž…์€ JSONํƒ€์ž…์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. 
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. 

์•ž์— @ResponseBody๋ฅผ ๋ถ™์—ฌ์„œ ๊ฐ์ฒด ํƒ€์ž…์ž„์„ ์•Œ๋ฆฐ๋‹ค.







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