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๐ŸŒณ๐Ÿ˜Š

[61] 230327 Spring: ์ดํด๋ฆฝ์Šค์— ์Šคํ”„๋ง ์„ค์น˜ํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 61์ผ] ๋ณธ๋ฌธ

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

[61] 230327 Spring: ์ดํด๋ฆฝ์Šค์— ์Šคํ”„๋ง ์„ค์น˜ํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 61์ผ]

yjyuwisely 2023. 3. 27. 14:09

230327 Mon 61st 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


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


์ž๋ฐ”์—์„œ๋Š” ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ํ‘œ์ค€์ด๋‹ค.


์Šคํ”„๋ง ์„ค์น˜ํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

์šฐ์„ , ์ž๋ฐ”๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
- ์ดํด๋ฆฝ์Šค: STS ํ”Œ๋Ÿฌ๊ทธ์ธ ์ถ”๊ฐ€ ์„ค์น˜ ํ›„ ์‚ฌ์šฉ
- STS์˜ ๊ฒฝ์šฐ: ๋ณ„๋„๋กœ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์••์ถ• ํ•ด์ œ 

์ดํด๋ฆฝ์Šค ๋ฒ„์ „์„ ํ™•์ธํ•œ๋‹ค.

์ฐธ๊ณ : https://curiousing.tistory.com/17

4.8 ๋ฒ„์ „์ด๋‹ค.

Help - Install New Software์„ ๋ˆ„๋ฅธ๋‹ค.

add ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์„œ ์•„๋ž˜ ๋งํฌ ์ฃผ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

http://download.springsource.com/release/TOOLS/update/e4.8

์„ค์น˜ ์™„๋ฃŒ

์ดํด๋ฆฝ์Šค์— ์Šคํ”„๋ง ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋๋‹ค.

SpringEx๋ฅผ Workspace์— ์˜ฎ๊ธด๋‹ค. 

File - Import Projects from File System or Archive๋ฅผ ๋ˆ„๋ฅธ๋‹ค.


src - test: ๋ง ๊ทธ๋Œ€๋กœ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ณณ
src - main: ๊ตฌํ˜„ํ•˜๋Š” ๊ณณ

src - main- webapp - WEB-INF - views์— jsp ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.


src - main - webapp - resources์— css, js ํŒŒ์ผ์„ ๋„ฃ๋Š”๋‹ค.


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—ฐ๊ฒฐ ๊ฒฝ๋กœ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊นŒ์ง€๊ฐ€ MVC ์ค‘ V์ด๋‹ค.


์ปจํŠธ๋กค๋Ÿฌ๋Š” ์‚ฌ์šฉ์ž ํ™”๋ฉด๊ณผ ์„œ๋ฒ„๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
๋ชจ๋ธ 2์˜ M, C๊ฐ€ Java Resources์— ์žˆ๋‹ค.


MVC ์ค‘ C(์ปจํŠธ๋กค๋Ÿฌ: ์‚ฌ์šฉ์ž ํ™”๋ฉด๊ณผ ์„œ๋ฒ„๋ฅผ ์—ฐ๊ฒฐ)์ด๋‹ค.
์ปจํŠธ๋กค๋Ÿฌ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋‚ด๋ฆฐ ๋ช…๋ น์„ ์ œ์ผ ์ฒ˜์Œ ์ง„์ž…ํ•˜๋ฏ€๋กœ ์ง„์ž…์ ์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.


MVC ์ค‘ M(๋ชจ๋ธ)์ด๋‹ค.


ํ”„๋กœ์ ํŠธ๋ฅผ ํ•  ๋•Œ ๋˜‘๊ฐ™์€ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์œผ๋กœ ๋งŒ๋“ ๋‹ค. (์ด๋ฆ„ ๋‹ค๋ฅด๋ฉด ํ•ฉ์น  ๋•Œ ์–ด๊ธ‹๋‚œ๋‹ค.)


ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์„ ๋งŒ๋“ค๋ฉด org.hj.controller๊ฐ€ ๋œ๋‹ค.


HomeController.java๋Š” ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์ ธ์žˆ๋Š” ๊ฑฐ๊ณ (MVC์˜ C), ๋‚˜๋จธ์ง€๋Š” ํ•„์š”์— ์˜ํ•ด์„œ ๋งŒ๋“  ๊ฒƒ์ด๋‹ค.


JSP์™€ ๋‹ค๋ฅด๊ฒŒ, Spring์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์„ ํƒํ•ด์„œ Run As - Run on Server๋ฅผ ๋ˆ„๋ฅธ๋‹ค.
์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ JSP๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค. (๋ฌด์กฐ๊ฑด ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๊ฑฐ์ณ์„œ ๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค.)


์•„๋ž˜ ์ฐฝ์ด ๋‚˜์˜จ๋‹ค. 


์ฝ˜์†”๋งŒ ์ž˜ ๋ด๋„ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ๋œ๋‹ค.
root-context.xml: example์„ sample๋กœ ๋ฐ”๊พผ๋‹ค.

	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>

๋นจ๊ฐ„ ๋Š๋‚Œํ‘œ ๋–ด์„ ๋•Œ Maven - Update Project ๋ˆ„๋ฅธ๋‹ค.


GR์€ ๊ทธ๋ƒฅ ์ปดํ“จํ„ฐ ์ด๋ฆ„์ด๊ณ  .m2 - repository๊ฐ€ ์ž๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

๊ฒฐ๊ณผ: ์„œ๋ฒ„์— ์—ฐ๊ฒฐ๋ผ์„œ ํŽ˜์ด์ง€๊ฐ€ ๋–ด๋‹ค.


์ด ์žฅ์€ ์ปจํŠธ๋กค๋Ÿฌ์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค. 

www.naver.com -> ๋„๋ฉ”์ธ ๋„ค์ž„
๋กœ์ปฌ ํ˜ธ์ŠคํŠธ -> ์ „์šฉ ๋ฒˆํ˜ธ
๋„๋ฉ”์ธ ๋„ค์ž„์ด ์—†์œผ๋ฏ€๋กœ ํ†ฐ์บฃ์—์„œ ์„ธํŒ…ํ•œ ๊ฒŒ ๋กœ์ปฌํ˜ธ์ŠคํŠธ (localhost:8080/)์ด๋‹ค. 

http://localhost:8081/controller/
         ์„œ๋ฒ„์ด๋ฆ„(๋„๋ฉ”์ธ)/contextpath

contextpath๋Š” ํ†ฐ์บฃ์—์„œ ํ™•์ธํ•œ๋‹ค.
์ดํด๋ฆฝ์Šค์˜ Servers๋ฅผ ๋”๋ธ” ํด๋ฆญํ•ด์„œ ์•„๋ž˜ Modules ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜ ์ •๋ณด๊ฐ€ ๋‚˜์˜จ๋‹ค.
URL์ฃผ์†Œ์— /controller๊ฐ€ ๋‚˜์˜ค๋Š” ์ด์œ ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

JSP์™€ Spring์ด ์ค‘๋ณต์€ ์•„๋‹ˆ๋‹ค.


Edit๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์ด๋ฆ„์„ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ๋‹ค. ์„œ๋ฒ„ ์ฃฝ์ด๊ณ  ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ฉด URL ์ฃผ์†Œ๊ฐ€ ๋ฐ”๋€๋‹ค. (์‹œ๊ฐ„์€ ๊ฑธ๋ฆฐ๋‹ค.)


๊ฒฐ๊ณผ) URL ์ฃผ์†Œ๊ฐ€ ๋ฐ”๋€Œ์—ˆ๋‹ค. (localhost:8081/controller/ -> localhost:8081)


์œ„ ๊ฒฐ๊ณผ๋Š” home.jsp์—์„œ ๋‚˜์˜จ ๊ฒƒ์ด๊ณ ,
ํ™ˆ ์ปจํŠธ๋กค๋Ÿฌ์— ์•„๋ž˜ ๊ฒฝ๋กœ๋กœ ์‹คํ–‰์‹œ์ผœ์„œ ๋‚˜์˜จ ๊ฒฐ๊ณผ์ด๋‹ค.
(๊ฒฝ๋กœ: src - main- webapp - WEB-INF - views)

home.jsp์˜ ์œ„์น˜์ด๋‹ค.


์ฝ”๋“œ) home.jsp

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";
		}
}

์•„๋ž˜๋Š” ์„œ๋ฒ„ ์—ญํ• ์„ ํ•˜๋Š” ํŒŒ์ผ์ด๋‹ค. servlet-context.xml
(๊ฒฝ๋กœ: SpringEx - src - main - webapp - WEB-INF - spring - appServlet)

servlet-context.xml์˜ ๊ฒฝ๋กœ

์ฝ”๋“œ) servlet-context.xml
์•ž์˜ home.jsp์˜ ๊ฒฝ๋กœ 
/WEB-INF/views/home.jsp
๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<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">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="org.hj.controller" />
	
	<beans:bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
	</beans:bean>

</beans:beans>

http://localhost:8080/์€ ๊ณ ์ •์ด๋‹ค.

์Šคํ”„๋งํ•œํ…Œ ๋ช…๋ น์„ ์ฃผ๋ฉด ์Šคํ”„๋ง์€ ์ €๋ ‡๊ฒŒ ์‹คํ–‰ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

URL ์ฃผ์†Œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด return์ด ์—†์–ด๋„ ๋œ๋‹ค. 
URL ์ฃผ์†Œ์™€ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๋‹ค๋ฅด๋ฉด return์„ ์ค€๋‹ค.


1๋ฒˆ ์ฝ”๋“œ) http://localhost:8080/sample/aaa
=> value="sample", value="aaa"
views - sample - aaa.jsp๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.
(return "sample/aaa"์™€ ๊ฐ™๋‹ค.)

2๋ฒˆ ์ฝ”๋“œ) bbb.jsp๋ฅผ ์‹คํ–‰ํ•ด์•ผํ•˜๋ฏ€๋กœ return "bbb";๋ฅผ ์“ด๋‹ค. 

3๋ฒˆ ์ฝ”๋“œ)  ccc.jsp๋ฅผ ์‹คํ–‰ํ•ด์•ผํ•˜๋ฏ€๋กœ return "sample/ccc";๋ฅผ ์“ด๋‹ค. 

=> C: ์ด ์—ญํ• ์€ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ํ•œ๋‹ค.
V: views ํด๋” ์•ˆ์— ์žˆ๋Š” ๊ฒƒ
M: ๋ชจ๋ธ ํŒจํ‚ค์ง€ ์•ˆ์— ์žˆ๋Š” ๊ฒƒ


http://localhost:8080/sample/aaa?id=aaa
id=aaa๋ฅผ ๋ณด๋‚ธ๋‹ค.

๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ € ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.
1)๋ฒˆ ์ฝ”๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ String id๋กœ ์„ค์ •ํ•œ๋‹ค.

public void basic(String id){
	Logger.info("sample/aaa");
}
}

pom.xml์˜ ์ž๋ฐ” ๋ฒ„์ „์„ 1.8๋กœ ๋ณ€๊ฒฝํ•˜๋ผ๊ณ  ํ•œ๋‹ค. (ํ˜„์žฌ ์ปดํ“จํ„ฐ๋Š” 1.8๋ฒ„์ „์ด์–ด์„œ ๋ณ€๊ฒฝ ์•ˆ ํ–ˆ๋‹ค.)

mysql ์ดํ•˜ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•˜๋ฉด jar ํŒŒ์ผ์„ ์ง์ ‘ ๋„ฃ๋Š” ๋Œ€์‹  ์•Œ์•„์„œ ์ˆ˜์ •ํ•ด์ค€๋‹ค๊ณ  ํ•œ๋‹ค.

์ฝ”๋“œ) pom.xml

		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>8.0.27</version>
		</dependency>

https://mvnrepository.com/

https://mvnrepository.com/artifact/mysql/mysql-connector-java

์œ„ ์ฝ”๋“œ๋ฅผ pom.xml์— ๋ถ™์—ฌ๋„ฃ์œผ๋ฉด jar ํŒŒ์ผ์„ ๋‹ค์šด ๋ฐ›๊ฒŒ ํ•œ๋‹ค. 

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.32</version>
</dependency>

https://mvnrepository.com/artifact/com.zaxxer/HikariCP


์ฝ”๋“œ) pom.xml์—์„œ ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ

		<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
		<dependency>
		    <groupId>com.zaxxer</groupId>
		    <artifactId>HikariCP</artifactId>
		    <version>2.7.4</version>
		</dependency>


์ฝ”๋“œ) root-context.xml์—์„œ ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ

	<!-- Root Context: defines shared resources visible to all other web components -->
	<!-- DBCP(DB Connection Pool) -->
	
	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
    
    	<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
		<constructor-arg ref="hikariConfig" />
	</bean>

์ฐธ๊ณ : https://oingdaddy.tistory.com/13


์ฐธ๊ณ (Mybatis์™€ ์Šคํ”„๋ง ์—ฐ๋™ํ•˜๊ธฐ): https://lifere.tistory.com/117

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