Spring

[Spring] welcome page ๋งŒ๋“ค๊ธฐ

soowitty 2024. 1. 3. 16:08

๐Ÿ’ก ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

1. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

https://start.spring.io/์— ์ ‘์†ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

start.spring.io

spring boot๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ spring ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์‚ฌ์ดํŠธ์ด๋‹ค.

 

 

ํ”„๋กœ์ ํŠธ ์„ ํƒ

  • Project : Gradle - Groovy
  • Spring Boot : 3.x.x
  • Language : Java
  • Packaging : Jar
  • Java : 17 or 21

Project Metdata

  • groupId : hello
  • artifactId : hello-spring

์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด Name๊ณผ Package name์€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

 

 

Dependencies

์—ฌ๊ธฐ์„œ ๋ถˆ๋Ÿฌ์˜ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ ํƒํ•ด์ฃผ๋ฉด ๋œ๋‹ค. Spring Web๊ณผ Thymeleaf๋ฅผ ์„ ํƒํ•œ๋‹ค.

Thymeleaf๋Š” html์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋‹ค.

 


 

2. ๋™์ž‘ ํ™•์ธ

 

1) HelloSpringApplication.java ํŒŒ์ผ์˜ ๊ธฐ๋ณธ main ๋ฉ”์†Œ๋“œ ์‹คํ–‰ํ•œ๋‹ค.

main ๋ฉ”์†Œ๋“œ ์˜†์˜ โ–ถ ํ‘œ์‹œ๋ฅผ ๋ˆ„๋ฅด๋ฉด main ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2) http://localhost:8080 ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜์˜ค๋ฉด ์„ฑ๊ณตํ•œ ๊ฒƒ์ด๋‹ค.

 

์‹คํ–‰ ์›๋ฆฌ

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๊ฒƒ์€ ์–ด๋–ป๊ฒŒ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ผ๊นŒ? 

@SpringBootApplication
public class HelloSpringApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloSpringApplication.class, Arrays.toString(args));
	}

}

main ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด SpringApplication.run(HelloSpringApplication.class); ๊ฐ€ ๋™์ž‘ํ•˜๊ณ , ๊ทธ๋Ÿฌ๋ฉด SpringBoot ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋œ๋‹ค. ์Šคํ”„๋ง๋ถ€ํŠธ๋Š” tomcat์ด๋ผ๋Š” ์›น ์„œ๋ฒ„๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ž๋™์œผ๋กœ tomcat ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ ์‹œํ‚ค๋ฉด์„œ SpringBoot๊ฐ€ ๋™์ž‘ํ•œ๋‹ค.

 

 


 

3. Welcome Page ๋งŒ๋“ค๊ธฐ

์ •์ ์œผ๋กœ ์‹คํ–‰

๊ฐ„๋‹จํ•œ html ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์—ฌ ์ •์ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž. 

๋จผ์ €, src/main/resources/static์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ index.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณธ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello~~
<a href="/hello">hello</a>
</body>
</html>

์ด๋ ‡๊ฒŒ ๋œจ๋ฉด ์„ฑ๊ณต์ด๋‹ค.

 

๋™์ ์œผ๋กœ ์‹คํ–‰๋˜๊ฒŒ ํ•ด๋ณด๊ธฐ

๊ทธ๋Ÿผ ์ด์ œ ๋™์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž!

 

src/main/java/hello.hellospring/controller์— HelloController.java ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

package hello.hellospring.controller;

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

@Controller
public class HelloController {
    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data","hello~~");
        return "hello";
    }
}

 

 

src/main/resources/static์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ hello.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"><head>
  <title>Hello</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'์•ˆ๋…•ํ•˜์„ธ์š”. ' + ${data}" >์•ˆ๋…•ํ•˜์„ธ์š”. ์†๋‹˜</p>
</body>
</html>

 

 

์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ localhost:8080/hello์— ์ ‘์†ํ–ˆ์„ ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋œจ๋ฉด ์„ฑ๊ณต์ด๋‹ค!

 

์ฝ”๋“œ๋ฅผ ๋œฏ์–ด์„œ ๋™์ž‘ ๊ณผ์ •์„ ์ดํ•ดํ•ด๋ณด์ž.

 

๋จผ์ €, @GetMapping("hello")์˜ ์—ญํ• ์€ ๋ญ˜๊นŒ? ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ /hello๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์•„๋ž˜์˜ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ์—ฌ๊ธฐ์—์„œ๋Š” hello ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

๋‹ค์Œ์œผ๋กœ model.addAttribute("data", "hello~~");  ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž. addAttribute๋Š” (key, value) ํ˜•ํƒœ์ด๋‹ค. ์ฆ‰, key๊ฐ€ data, value๊ฐ€ hello~~์ธ ๊ฒƒ์ด๋‹ค. (view ์ฝ”๋“œ(htmlํŒŒ์ผ)์—์„œ ${data}๋กœ hello~~๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ๋œ๋‹ค.) 

๋งˆ์ง€๋ง‰์œผ๋กœ, return "hello";๋Š” hello.html์„ ๋ Œ๋”๋งํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค. SpringBoot๋Š” ํ•ด๋‹น return ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด resources/templates/hello.html์„ ์ฐพ์•„์„œ ๋ Œ๋”๋งํ•˜๋„๋ก ์„ธํŒ…๋˜์–ด ์žˆ๋‹ค.

 

 

 

๋งˆ์ง€๋ง‰์œผ๋กœ, ๊ทธ๋ฆผ์œผ๋กœ ๋™์ž‘ ๊ณผ์ •์„ ์ดํ•ดํ•ด๋ณด์ž.

 

1. ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ localhost:8080/hello๋ฅผ ์š”์ฒญํ•œ๋‹ค.

2. ๋‚ด์žฅ tomcat ์„œ๋ฒ„๊ฐ€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ ์š”์ฒญ์„ ๋„˜๊ธด๋‹ค.

3. ์ปจํ…Œ์ด๋„ˆ๋Š” Controller์—์„œ hello์— ๋งตํ•‘๋œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. @GetMapping()์ด ์กด์žฌํ•œ๋‹ค๋ฉด ๋งตํ•‘๋œ hello() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. 

4. return ๊ฐ’์ธ hello๋ฅผ ๋ณด๋‚ด๊ณ , ๋™์‹œ์— model์— (key:value)ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ์ธ (data:hello~~) ๋ฅผ ๋„ฃ์–ด ๋ณด๋‚ธ๋‹ค.

5. src/main/resources/templates์—์„œ View์— ํ•ด๋‹นํ•˜๋Š” hello.html์„ ์ฐพ์•„์ฃผ๊ณ , ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” viewResolver๊ฐ€ ๋™์ž‘ํ•œ๋‹ค.

SpringBoot ํ…œํ”Œ๋ฆฟ ์—”์ง„์€ ๊ธฐ๋ณธ์ ์œผ๋กœ viewName ๋งคํ•‘์ด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ viewName ๋งคํ•‘์ด๋ž€, 'resources:templates + {viewName} + .html' ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

6. ํ…œํ”Œ๋ฆฟ์—”์ง„(thymeleaf)์€ html ํŒŒ์ผ์„ ๋ฐ›์•„์„œ ๋ณ€ํ™˜ํ•œ ํ›„์— ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

 

'Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Spring Boot] JPA ๋ถ€ํ„ฐ Spring Data JPA๊นŒ์ง€  (0) 2024.04.14
[Spring] AOP  (0) 2024.01.22