IT EDU/SPRING

[Spring] ์Šคํ”„๋ง ๋ถ€ํŠธ - Spring Boot ์„ค์น˜ ๋ฐ ๊ธฐ๋ณธ ์„ธํŒ…

yoonhoou 2022. 3. 18.
728x90

 

 

1. Spring Boot install

 

eclipse - help - Eclipse Marketplace

 

aka Spring Tool Suite 4 install

 

Confirm

 

 

accept - Finish

 

install ํ›„ ์žฌ๋ถ€ํŒ…!

 

 

๐Ÿ˜ต์ฃผ์˜์‚ฌํ•ญ

1๏ธโƒฃ ๋‚ด์žฅ Tomcat์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”
Tomcat์€ ๋น„ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•œ๋‹ค. (์„œ๋ฒ„ ์ถฉ๋Œ ๋ฐฉ์ง€)

2๏ธโƒฃ index.jsp๋„ Controller๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

3๏ธโƒฃ ๊ธฐ์กด์— View๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด Views ํด๋” ํ•˜์œ„์— ์ƒ์„ฑํ–ˆ๋‹ค.
Spring Boot ์—์„œ๋Š” index.jsp๋„ Views ํด๋” ํ•˜์œ„์— ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

4๏ธโƒฃ 8080 port๋กœ ๊ณ„์† ์ž‘์—…ํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ถฉ๋Œ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
8081 or 8082 ๋“ฑ์˜ port๋ฅผ ์‚ฌ์šฉํ•˜์ž.

5๏ธโƒฃ ์„œ๋ฒ„ ์‹คํ–‰ ์‹œ page๊ฐ€ ์ž๋™์œผ๋กœ ์—ด๋ฆฌ์‹œ ์•Š๋Š”๋‹ค.
localhost:8081 ๋“ฑ์œผ๋กœ web์— ์ง์ ‘ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 

 

2. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

 

 

Name : Sts01

Java Version : 11

Packaging : War

Package : Pack

 

 

web - Spring Web ์ฒดํฌ - Finish

 

 

3. ๊ธฐ๋ณธ ์„ค์ •

 

 

 

pom.xml

<version>2.6.4</version> ์‚ฌ์ด์—์„œ ๋ฒ„์ „ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

 

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€

1. jstl

2. ๋‚ด์žฅํ†ฐ์บฃ

3. auto refresh (dev-tools)

dev-tool์„ ์“ฐ๋ฉด ์•ˆ ๋˜๋Š” ์ƒํ™ฉ์ด ์˜ค๋ฉด 

์ฃผ์„์„ ์žก์•„์ฃผ๋ฉด ๋œ๋‹ค.

 

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
</dependencies>

 

 

application.properties ์„ค์ •

application.properties

 

 

application.properties ์„ค์ • ๋ฐฉ๋ฒ•

 

1๏ธโƒฃ .properties ๋ฐฉ๋ฒ• (โœ…)

# ํฌํŠธ ์„ค์ •(๋‚ด์žฅ ํ†ฐ์บฃ์ด ์‚ฌ์šฉํ•  ํฌํŠธ ๋ฒˆํ˜ธ)
server.port = 8081

# view ๊ฒฝ๋กœ ์„ค์ •
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp

 

 

Save as UTF-8 ์„ ํƒ

 

 

index.jsp ์ƒ์„ฑ

src/main/webapp/WEB-INF/views/index.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h3>index.jsp</h3>
	<a href="t1">๋งํฌ 1</a>
</body>
</html>

 

 

index์˜ controller ์ƒ์„ฑ

src/main ์—์„œ controller ์ƒ์„ฑ

 

 

package Pack;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Tiger {
	@RequestMapping("/")
	public String f0() {
		System.out.println("๊ณง index.jsp๊ฐ€ ์‹คํ–‰๋ ๊ฒƒ์ž„.");
		return "index";
	}
}

ํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ - Run as - Spring Boot App

 

 

๐Ÿคท‍โ™‚๏ธDefalut project ๋ณต์‚ฌํ•  ๋•Œ๐Ÿคท‍โ™‚๏ธ

1๏ธโƒฃ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ

2๏ธโƒฃpom.xml
<artifactId>Sts01</artifactId>
-> <artifactId>Sts02</artifactId>

3๏ธโƒฃsrc/main/java/Sts01Application 
-> src/main/java/Sts02Application 

4๏ธโƒฃํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ -> PROPERTIES -> Web project setting
-> Sts02

๋Œ“๊ธ€