ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] Spring Boot Project ์ƒ์„ฑ, ๋นŒ๋“œ ๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ํ•˜๊ธฐ (+ View)
    ๐ŸณDev/Dev 2022. 4. 14. 11:18

     

    ๊น€์˜ํ•œ ๋‹˜์˜ <์Šคํ”„๋ง ์ž…๋ฌธ - ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ > ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

     


    ์ƒ์„ฑํ•˜๊ธฐ

     

    ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์„ ์œ„ํ•ด Spring Initializer๋กœ ์ด๋™ํ•˜์ž

    https://start.spring.io/

     

     

    Project : ๋นŒ๋“œ ๊ด€๋ฆฌ ๋„๊ตฌ์— ๋Œ€ํ•œ ๊ฒฐ์ •

    Language : ์–ธ์–ด์— ๋Œ€ํ•œ ๊ฒฐ์ •

    Spring Boot : Spring Boot ๋ฒ„์ „์— ๋Œ€ํ•œ ๊ฒฐ์ •

    Project Metadata : ํ”„๋กœ์ ํŠธ MetaData๋ฅผ ๊ฒฐ์ •, Group ์ด๋ฆ„๊ณผ Artifact ์ด๋ฆ„, Package ํ™•์žฅ์ž๋ช…๊ณผ Java ๋ฒ„์ „์„ ๋„ฃ์–ด์ค€๋‹ค.

    Dependencies : ํ”„๋กœ์ ํŠธ์— ์ถ”๊ฐ€ํ•  ์˜์กด์„ฑ์„ ๊ฒฐ์ •, ๋‚˜๋Š” Spring Web๊ณผ Thymeleaf๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.

     

     

     

    ์„ค์ • ํ›„์— GENERATE ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์••์ถ• ํŒŒ์ผ์ด ๋‚ด๋ ค ๋ฐ›์•„์ง„๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ์••์ถ•์„ ํ‘ผ ํŒŒ์ผ์„ IntelliJ๋ฅผ ํ†ตํ•ด์„œ ์—ด์–ด์ค€๋‹ค.

     

     

    build.gradle ํŒŒ์ผ์— ๋“ค์–ด๊ฐ€๋ฉด

    ์•„๊นŒ ์ƒ์„ฑํ•  ๋•Œ ์„ค์ •ํ•œ ๊ฐ’๋“ค์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    dependencies์˜ ๊ฒฝ์šฐ

    ๋‚ด๊ฐ€ ์ถ”๊ฐ€ํ•œ thymeleaf์™€ web์™ธ์— test๊ฐ€ ์ž๋™์œผ๋กœ ๋“ค์–ด์žˆ๋‹ค.

     

    repositories๋Š” ๊ฐ์ข… ์˜์กด์„ฑ๋“ค์„ ์–ด๋””์„œ ๋ฐ›์•„์˜ฌ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

    mavenCentral๊ณผ jcenter๊ฐ€ ๋Œ€ํ‘œ์ ์ด๋ฉฐ,

    ์šฐ๋ฆฌ๋Š” ์ „์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

     

    https://mvnrepository.com/repos/central

     

    IntelliJ์—์„œ run์„ ์‹คํ–‰ํ•˜๋ฉด

    ์ž๋™์ ์œผ๋กœ ์›น์„œ๋ฒ„์ธ tomcat์ด ์‹คํ–‰๋˜๊ณ 

    ์ด๋Š” localhost:8080์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

     


    + View

     

    ์œ„์™€ ๊ฐ™์€ Error Page๊ฐ€ ์‹ซ๋‹ค๋ฉด

    src/main/resources/static ์— index.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์„œ ๋„ฃ์–ด์ค€๋‹ค.

     

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

     

    static/index.html์€ ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” Welcome Page ๊ธฐ๋Šฅ์ด๋‹ค

    https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.spring-mvc.welcome-page

     

    Web

    Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

    docs.spring.io

     

     tymeleaf๋ฅผ ์ด์šฉํ•˜๋ฉด ๋™์ž‘ํ•˜๋Š” View๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์ด๋ฒˆ์—๋Š” resources/templates์— 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}">Hello</p>
    </body>
    </html>
    

     

    ๋‘๋ฒˆ์งธ ์ค„<html>์—์„œ thymeleaf๋ฅผ ์„ ์–ธํ•˜๋ฉด, ์šฐ๋ฆฌ๋Š” ํƒ€์ž„๋ฆฌํ”„ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  8๋ฒˆ์งธ ์ค„<p>์„ ๋ณด๋ฉด ์šฐ๋ฆฌ๋Š” th๋ฅผ ํ†ตํ•ด, ํƒ€์ž„๋ฆฌํ”„ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

    ์ดํ›„ ${data}์€ ๋ณ€์ˆ˜์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.

     

     

    ๋‹ค์Œ์€ java/hello/hellospring์— controller ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค๊ณ  HelloController.java๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

     

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

     

    ๊ฐ€์žฅ ๋จผ์ € Annotation(@)์„ ํ†ตํ•ด ํ•ด๋‹น ๋ถ€๋ถ„์ด Controller์ž„์„ ์„ ์–ธํ•œ๋‹ค.

    @GetMapping์˜ ๊ฒฝ์šฐ, ์ธ์ž๋กœ ๋“ค์–ด ์žˆ๋Š” ๊ฐ’์œผ๋กœ ์š”์ฒญ์„ ๊ตฌ๋ถ„ํ•œ๋‹ค.

     

    5๋ฒˆ์งธ ์ค„์˜ data๋Š” ์œ„์˜ thymeleaf๋กœ ์„ ์–ธํ•œ data๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ,

    return๊ฐ’์€ templates ํด๋” ์•ˆ์˜ ํ•ด๋‹น ์ ์šฉํ•  ํŒŒ์ผ ์ด๋ฆ„์„ ์˜๋ฏธํ•œ๋‹ค.

    (Controller์—์„œ return์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ ํ™”๋ฉด์„ ViewResolver๊ฐ€ ์ฐพ์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค)

     

     

    ์ฆ‰, localhost:8080/hello๋ผ๊ณ  ์š”์ฒญํ•˜๋ฉด hello๊ฐ€ ์‹คํ–‰๋˜์–ด, hello.html์˜ data ๋ถ€๋ถ„์— hello๋ฅผ ๋„ฃ์–ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

     


    ๋นŒ๋“œ ๋ฐ ์‹คํ–‰

     

     

    ./gradlew clean build
    ./gradlew build

     

    ์œ„์˜ ๋ช…๋ น์–ด๋กœ build ํ•œ๋‹ค

    (window์˜ powershell ํ™˜๊ฒฝ)

     

     

    cd build/libs
    java -jar project-name.jar

     

    ์ดํ›„ build/libs ํด๋”์˜ ์ƒ์„ฑ๋œ jar ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค!

     

    ์ž˜ ์‹คํ–‰๋˜๋Š” localhost:8080๋„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

Designed by Tistory.