Skip to content

TIL – 2024/10/22 Spring View Engine๊ณผ Build

  • by

Spring Static Page

์Šคํ”„๋ง ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค๋ช…ํ•œ๋‹ค.

Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.

This only acts as a fallback for actual index routes defined by the application. The ordering is defined by the order ofย HandlerMappingย beans which is by default the following:

์ •์ , ํ…œํ”Œ๋ฆฟํ™”๋œ ์›ฐ์ปด ํŽ˜์ด์ง€๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•œ๋‹ค. ๋จผ์ €, index.html ํŒŒ์ผ์ด resources/static ๋””๋ ‰ํ† ๋ฆฌ์— ์œ„์น˜ํ•ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์—†๋Š” ๊ฒฝ์šฐ์—” index template(์—”์ง„ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‹ค๋ฆ„)์„ ์ฐพ๊ฒŒ ๋œ๋‹ค. ํ•˜๋‚˜๋ผ๋„ ๋ฐœ๊ฒฌ๋˜๋ฉด ๊ทธ๊ฒŒ ์›ฐ์ปดํŽ˜์ด์ง€๋กœ ์ž๋™ ๋“ฑ๋ก์ด ๋œ๋‹ค.

๋‹น์—ฐํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๋ฐฉ์‹๋ณด๋‹ค๋Š” ์‹ค์ œ index ๋ผ์šฐํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์ด๋Ÿฌํ•œ ์ˆœ์„œ๋Š” HandlerMapping์ด๋ž€ ๋นˆ์— ๊ธฐ๋ณธ ๋“ฑ๋ก๋˜์–ด์žˆ๋‹ค๊ณ  ๋ช…์‹œ๋˜์–ด์žˆ๋‹ค.

Spring View Engine

Spring์—์„œ๋Š” ๋‹ค์–‘ํ•œ View Engine์„ ์ง€์›ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ ์—”์ง„์˜ ์ด๋ฆ„์€ Thymeleaf๋กœ ๊ฐ„๋‹จํ•œ 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>

์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด viewResolver๊ฐ€ ์ž‘๋™ํ•˜์—ฌ Template Engine์—๊ฒŒ ๋ฐ์ดํ„ฐ์™€ HTML๋กœ ๋งŒ๋“ค๋ผ๋Š” ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ , ์ด๋ฅผ ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ๋ณ€ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.

์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

package hello.hello_spring.controller;

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

@Controller
public class HelloController {

    /**
     * thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ๋™์ž‘์‹œํ‚จ๋‹ค.
     * ์š”์ฒญ -> ํ†ฐ์บฃ ์„œ๋ฒ„ (/hello ๋ผ์šฐํŒ…) -> ์Šคํ”„๋ง๋ถ€ํŠธ helloController
     * -> viewResolver(ํ…œํ”Œ๋ฆฟ ์—”์ง„ ์ฒ˜๋ฆฌ) -> html ๋ณ€ํ™˜ -> ๋ฐ˜ํ™˜
     *
     * ํŒจํ„ด
     * resources:templates/{ViewName}.html
     * @param model
     * @return
     */
    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "Hello!!");
        return "hello";
    }
}

Build, Distribute, and Handle Fallback

gradle์€ ์Šคํ”„๋ง์„ ๊ตฌ์„ฑํ•˜๋Š” ์„ค์ •์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์šฐ๋ฆฐ ์Šคํ”„๋ง ํŒŒ์ผ์„ ํ•˜๋‚˜์˜ .jarํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ๋ง˜๊ป ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๋‹ค.

./gradlew build # ํ”„๋กœ์ ํŠธ ๋นŒ๋“œํ•˜๊ธฐ

ํ•ด๋‹น ๋ช…๋ น์„ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ .jarํŒŒ์ผ์€ /build/libs์•ˆ์— ์œ„์น˜ํ•œ๋‹ค. Java ๊ฐ€์ƒ๋จธ์‹ ์ด ์„ค์น˜๋œ ์ปดํ“จํ„ฐ๋ผ๋ฉด ์–ด๋””์„œ๋“  ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•ด์ง€๋Š” ๊ฒƒ์ด๋‹ค.

java -jar <๋งŒ๋“ค์–ด์ง„ ํŒŒ์ผ ์ด๋ฆ„> # ํ•ด๋‹น ํŒŒ์ผ์„ ๊ฐ€๋™

์Šคํ”„๋ง์ด ์‹คํ–‰์ด ์‹œ์ž‘๋  ๊ฒƒ์ด๋‹ค.

./gradlew clean

๋งŒ์•ฝ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด ํ”„๋กœ์ ํŠธ ์บ์‹œ, ํŒŒ์ผ ๋“ฑ์ด ์ „๋ถ€ ๋‚ ๋ผ๊ฐ€์„œ ์˜ค๋ฅ˜ ์ƒํ™ฉ์—์„œ ์š”๊ธดํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Leave a Reply

Your email address will not be published. Required fields are marked *