SpringMVC ๐ŸŒฑ View Template์— ๋Œ€ํ•œ ์ดํ•ด์™€ ์‚ฌ์šฉ๋ฒ•

Doridamยท2023๋…„ 6์›” 7์ผ
0

Spring๐ŸŒฑ & Boot ๐ŸŒผ

๋ชฉ๋ก ๋ณด๊ธฐ
12/12


View

SpringMVC์˜ View. View์˜ ์—ญํ• ์€ ์‚ฌ์šฉ์ž์—๊ฒŒ UI๋ฅผ ์ œ๊ณตํ•˜๊ฑฐ๋‚˜ ์ž…๋ ฅ์„ ๋ฐ›๊ณ , ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š”๊ฒƒ์— ์žˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ FrontframeWork๋ฅผ ์‚ฌ์šฉํ•ด์„œ RestAPI๋กœ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜ SSR(Server Side Rendering)์ธ Template Engine์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Template Engine์€ Thymeleaf, Apache Freemarker, Mustache, Groovy Templates ๋“ฑ์ด ์žˆ๋‹ค.


Template Engine

Template Engine์€ ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ HTML๋“ฑ์˜ ํ…œํ”Œ๋ฆฟ์„ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. Server์ธก์—์„œ ๋žœ๋”๋ง ๋˜๊ธฐ ๋•Œ๋ฌธ์— SSR(Server Side Rendering)์ด๋ผ ํ•˜๋ฉฐ ์„œ๋ฒ„์ธก์˜ ์ฝ”๋“œ์™€ HTML ๋ฌธ๋ฒ•์„ ์กฐํ•ฉํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค.


Thymeleaf

Spring์—์„œ ๊ถŒ์žฅํ•˜๋Š” Template Engine. HTML ๊ธฐ๋ฐ˜์˜ ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ•˜๋ฉฐ, thymeleaf์˜ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋žœ๋”๋งํ•œ๋‹ค. HTMLํŒŒ์ผ๋กœ ์ž‘์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋žœ๋”๋ง์„ ํ•˜์ง€ ์•Š๋”๋ผ๋„ ๋งˆํฌ์—…๋œ ํ…œํ”Œ๋ฆฟ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๊ณ  if, each ์™€ ๊ฐ™์€ ๋กœ์ง๋„ ์ง€์›ํ•œ๋‹ค.

์‚ฌ์šฉ๋ฒ•

1. dependency ์ถ”๊ฐ€

โ€ป ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ : SpringBoot 2.7.12 / IntelliJ IDE / Windows 10

// gradle์˜ ๊ฒฝ์šฐ build.gradle์— ์ถ”๊ฐ€
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
/
// maven ์˜ ๊ฒฝ์šฐ pom.xml ์— ์ถ”๊ฐ€
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

์œ„์— ํ•ด๋‹นํ•˜๋Š” dependency๋ฅผ ๊ฐ๊ฐ ๋นŒ๋“œํˆด์— ๋งž๊ฒŒ ์ถ”๊ฐ€ํ•œ๋‹ค.

2. application.properties์— ViewResolver ๊ด€๋ จ ์„ค์ • ์ถ”๊ฐ€

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html


templates ํด๋” ๋‚ด๋ถ€์— ํ™•์žฅ์ž๊ฐ€ .html์ธ ํŒŒ์ผ๋กœ ViewResolver๋ฅผ ์ง€์ •ํ•˜๋Š” ๋‚ด์šฉ์ด๋‹ค. ๋งŒ์•ฝ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋กœ ์ง€์ •ํ•œ๋‹ค๋ฉด ์ˆ˜์ • ํ›„ ์ €์žฅํ•œ๋‹ค. ํŒŒ์ผ์„ resources>templates์— ์ €์žฅํ•˜๋ฉด ์ž๋™์œผ๋กœ ViewResolver๊ฐ€ ์ž๋™์œผ๋กœ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์žก์•„์ฃผ๊ธฐ ๋•Œ๋ฌธ์— Controller์˜ return ๊ฐ’์—๋Š” ํŒŒ์ผ๋ช…๋งŒ ์ ์œผ๋ฉด ๋œ๋‹ค.

3. ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ ์–ธ

<html lang="ko" xmlns:th="http://www.thymeleaf.org">

HTML ํŒŒ์ผ ์ƒ๋‹จ์— th๋กœ thymeleaf์˜ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ ์–ธํ•œ๋‹ค.

4. ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.


Server ์ธก ์ฝ”๋“œ GetMapping์„ ํ†ตํ•ด test๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์œผ๋กœ "ํ…Œ์ŠคํŠธ์ž…๋‹ˆ๋‹ค." ๊ฐ’์„ ๋„ฃ์–ด testView๋ผ๋Š” ์ด๋ฆ„์˜ View ํŒŒ์ผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>TEST</title>
</head>
<body>
    <h1 th:text="${test}"></h1>
</body>
</html>

th:text="${test}" model.addAttribute๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋œ test๊ฐ€ ๊ฐ’์ด ๋ฐ”์ธ๋”ฉ๋˜๋„๋ก SpringEL์ธ ${๋ณ€์ˆ˜๋ช…}์„ ์‚ฌ์šฉํ•ด ํ…œํ”Œ๋ฆฟ์„ ์ž‘์„ฑํ–ˆ๋‹ค.


th:text ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ h1 ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜์Œ์—๋„ "ํ…Œ์ŠคํŠธ์ž…๋‹ˆ๋‹ค."๋ผ๋Š” ๊ฐ’์ด ๋‚˜์˜จ๊ฒƒ์„ ํ™•์ธํ•˜์˜€๋‹ค.
Server์—์„œ ๋ Œ๋”๋ง์‹œ ๊ฐ’์ด ๋ฐ”์ธ๋”ฉ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
๋˜ํ•œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ Style์— ์ ์šฉํ•˜๊ฑฐ๋‚˜ value์— ์ ์šฉํ•˜๋Š”๋“ฑ ๋‹ค์–‘ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ธฐ๋ณธ ๋ฌธ๋ฒ•

  • ๋ณ€์ˆ˜ ์„ ์–ธ : ${๋ณ€์ˆ˜๋ช…}
  • ๋ณ€์ˆ˜ ๊ฐ’ ์ถœ๋ ฅ: [[๋ณ€์ˆ˜๋ช…]]
  • ์กฐ๊ฑด๋ฌธ :
<div th:if="${์กฐ๊ฑด์‹}">
  ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ๋ณด์—ฌ์ง€๋Š” ๋‚ด์šฉ
</div>
<div th:unless="${์กฐ๊ฑด์‹}">
  ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ๋ณด์—ฌ์ง€๋Š” ๋‚ด์šฉ
</div>
  • ๋ฐ˜๋ณต๋ฌธ
<ul>
  <li th:each="item : ${์ปฌ๋ ‰์…˜}">
    [[item]]
  </li>
</ul>
  • ์†์„ฑ๊ฐ’ ์„ค์ •
<a th:href="@{๊ฒฝ๋กœ/URL}">๋งํฌ</a>
<img th:src="@{์ด๋ฏธ์ง€_๊ฒฝ๋กœ}" alt="์ด๋ฏธ์ง€">
  • ์ธํด๋ฃจ๋“œ (๋‹ค๋ฅธ ํ…œํ”Œ๋ฆฟ์„ ํฌํ•จ)
<div th:include="ํ…œํ”Œ๋ฆฟ_ํŒŒ์ผ๋ช… :: ์„น์…˜_์ด๋ฆ„"></div>

๋งํฌ๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ๋ฌธ๋ฒ•์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


Apache Freemarker

Apache Freemarker ๊ฐ™์€ ๊ฒฝ์šฐ Apache ์žฌ๋‹จ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ์˜คํ”ˆ์†Œ์Šค Template Engine ์ด๋ฉฐ ํŒŒ์ผ ํ™•์žฅ์ž๋ช…์€ .FTL(Freemarker Template Langueage)๋กœ ์ž‘์„ฑ๋œ๋‹ค. ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ, ๋งคํฌ๋กœ์™€ ๋ฉ”์†Œ๋“œ ๋“ฑ์˜ ์ž๋ฐ”์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”์†Œ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์‚ฌ์šฉ๋ฒ•

1. dependency ์ถ”๊ฐ€

โ€ป ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ : SpringBoot 2.7.12 / IntelliJ IDE / Windows 10

	// gradle์˜ ๊ฒฝ์šฐ
	implementation 'org.springframework.boot:spring-boot-starter-freemarker'
	// maven์˜ ๊ฒฝ์šฐ
	<dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>

2. application.properties ์„ค์ •

spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl


freemarker ๊ด€๋ จ ViewResolver๋ฅผ ์„ค์ • ํ•ด ์ค€๋‹ค. resource/templates ๊ธฐ๋ณธ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ง€๊ณ  .ftl ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ํŒŒ์ผ๋กœ ์„ค์ •ํ•œ๋‹ค.

3. ๋ฉ”์†Œ๋“œ ๋ฐ ํ…œํ”Œ๋ฆฟ ์ž‘์„ฑ




๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์˜ ๊ฒฝ์šฐ ${๋ณ€์ˆ˜๋ช…}์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฝ”๋“œ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ "ํ”„๋ฆฌ๋งˆ์ปค ํ…Œ์ŠคํŠธ์ž…๋‹ˆ๋‹ค."๊ฐ€ ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ธฐ๋ณธ ๋ฌธ๋ฒ•

  • ๋ณ€์ˆ˜์„ ์–ธ : <#assign ๋ณ€์ˆ˜๋ช… = ๊ฐ’>
  • ๋ณ€์ˆ˜ ์ฐธ์กฐ: ${๋ณ€์ˆ˜๋ช…}
  • ์กฐ๊ฑด๋ฌธ :
<#if ์กฐ๊ฑด์‹>
  ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ๋‚ด์šฉ
<#elseif ๋‹ค๋ฅธ_์กฐ๊ฑด์‹>
  ๋‹ค๋ฅธ ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ์‹คํ–‰ํ•  ๋‚ด์šฉ
<#else>
  ๋ชจ๋“  ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰ํ•  ๋‚ด์šฉ
</#if>
  • ๋ฐ˜๋ณต๋ฌธ :
<#list ๋ฆฌ์ŠคํŠธ as ๋ณ€์ˆ˜๋ช…>
  ๋ฐ˜๋ณตํ•  ๋‚ด์šฉ
</#list>
<#foreach ๋ณ€์ˆ˜๋ช… in ๋ฆฌ์ŠคํŠธ>
  ๋ฐ˜๋ณตํ•  ๋‚ด์šฉ
</#foreach>
<#list 1..10 as i>
  ๋ฐ˜๋ณตํ•  ๋‚ด์šฉ (${i})
</#list>
  • ํ•จ์ˆ˜
${๋ณ€์ˆ˜?upper_case}      // ๋ณ€์ˆ˜๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
${๋ณ€์ˆ˜?lower_case}      // ๋ณ€์ˆ˜๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
${๋ณ€์ˆ˜?length}          // ๋ณ€์ˆ˜์˜ ๊ธธ์ด ๋ฐ˜ํ™˜
${๋ณ€์ˆ˜?default("๊ธฐ๋ณธ๊ฐ’")} // ๋ณ€์ˆ˜๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’ ์ง€์ •
  • ์ธํด๋ฃจ๋“œ (๋‹ค๋ฅธ ํ…œํ”Œ๋ฆฟ์„ ํฌํ•จ)
<#include "๋‹ค๋ฅธ_ํ…œํ”Œ๋ฆฟ.ftl">

๋งํฌ๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.


Mustache

๊ฐ„๋‹จํ•˜๊ณ  ์ง๊ด€์ ์ธ Template Engine์ด๋ฉฐ ๋กœ์ง์ด ์—†๋Š” Logic-Less Templates ๋ผ ๋ถˆ๋ฆฐ๋‹ค. ์กฐ๊ฑด๋ฌธ์ด๋‚˜ ๋ฐ˜๋ณต๋ฌธ์€ ์žˆ์ง€๋งŒ ์ด๋Š” ํ…œํ”Œ๋ฆฟ์— ์ ์šฉ๋˜๊ธฐ ์ „์— ์„œ๋ฒ„ ๋˜๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ์ฒ˜๋ฆฌ๋˜๋ฏ€๋กœ ํ…œํ”Œ๋ฆฟ์—์„œ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์‚ฌ์šฉ๋ฒ•

1. dependency ์ถ”๊ฐ€

โ€ป ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ : SpringBoot 2.7.12 / IntelliJ IDE / Windows 10

	//gradle์˜ ๊ฒฝ์šฐ > build.gradle
    implementation 'com.github.spullara.mustache.java:compiler:0.9.6'
	//maven์˜ ๊ฒฝ์šฐ > pom.xml
	    <dependency>
        	<groupId>com.github.spullara.mustache.java</groupId>
        	<artifactId>compiler</artifactId>
        	<version>0.9.6</version>
    	</dependency>


mustache ๋Š” ๊ธฐ๋ณธ์ ์ธ ๊ฒฝ๋กœ๋กœ resources/templates๋ฅผ ์žก๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ViewResolver ์„ค์ •์€ ํ•„์š”์น˜ ์•Š๋‹ค. ํŒŒ์ผ ํ™•์žฅ์ž๋Š” .mustache ์ด๋‹ค.

2. ๋ฉ”์†Œ๋“œ ๋ฐ ํ…œํ”Œ๋ฆฟ ์ž‘์„ฑ




๊ธฐ๋ณธ ๋ฌธ๋ฒ•์€ {{๋ณ€์ˆ˜๋ช…}}์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค. #์„ ์‚ฌ์šฉํ•˜์—ฌ if๋ฌธ์ด๋‚˜ ๋ฐ˜๋ณต๋ฌธ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ง์ ‘์ ์ธ ๋กœ์ง์ด ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„์—์„œ ๋กœ์ง์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ํ›„ View๋กœ ์ „๋‹ฌํ•ด์•ผํ•˜๋ฉฐ ํ…œํ”Œ๋ฆฟ๊ณผ ๋กœ์ง์„ ๋ช…ํ™•ํžˆ ๋ถ„๋ฆฌ์‹œ์ผœ ์œ ์ง€๋ณด์ˆ˜์„ฑ๊ณผ ํ…œํ”Œ๋ฆฟ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์€ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋‹ค.
๊ธฐ๋ณธ ๋ฌธ๋ฒ•

  • ๋ณ€์ˆ˜ ์ถœ๋ ฅ: {{๋ณ€์ˆ˜๋ช…}}
  • ๋ณ€์ˆ˜ ๊ฐ’์ด HTML์ด๋‚˜ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ: {{{๋ณ€์ˆ˜๋ช…}}} (์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Œ)
  • ์กฐ๊ฑด์‹ :
{{#์กฐ๊ฑด์‹}}
  ์กฐ๊ฑด์ด ์ฐธ์ผ ๋•Œ ๋ณด์—ฌ์ง€๋Š” ๋‚ด์šฉ
{{/์กฐ๊ฑด์‹}}
{{^์กฐ๊ฑด์‹}}
  ์กฐ๊ฑด์ด ๊ฑฐ์ง“์ผ ๋•Œ ๋ณด์—ฌ์ง€๋Š” ๋‚ด์šฉ
{{/์กฐ๊ฑด์‹}}
  • ๋ฐ˜๋ณต๋ฌธ :
<ul>
  {{#๋ฐฐ์—ด๋ช…}}
    <li>{{.}}</li>
  {{/๋ฐฐ์—ด๋ช…}}
</ul>
  • ์†์„ฑ๊ฐ’ ์„ค์ • :
<a href="{{๊ฒฝ๋กœ/URL}}">๋งํฌ</a>
<img src="{{์ด๋ฏธ์ง€_๊ฒฝ๋กœ}}" alt="์ด๋ฏธ์ง€">
  • ํ…œํ”Œ๋ฆฟ ์ธํด๋ฃจ๋“œ :
{{> ํ…œํ”Œ๋ฆฟ_ํŒŒ์ผ๋ช…}}

Groovy Template

Groovy ์–ธ์–ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ Template Engine. Groovy ์Šคํฌ๋ฆฝํŒ… ์–ธ์–ด์™€ java์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ ์ธ ์ปจํ…์ธ ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. java์™€ ํ˜ธํ™˜์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— java์˜ ๋ฉ”์†Œ๋“œ๋‚˜ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ํ…œํ”Œ๋ฆฟ์˜ ์ƒ์†์„ ์ง€์›ํ•˜์—ฌ ์žฌ์‚ฌ์šฉ์— ์šฉ์ดํ•˜๋‹ค.

์‚ฌ์šฉ๋ฒ•

1. dependency ์ถ”๊ฐ€

โ€ป ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ : SpringBoot 2.7.12 / IntelliJ IDE / Windows 10

	// gradle > build.gradle
    implementation 'org.codehaus.groovy:groovy-templates:<version>'
	// maven > pom.xml
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-templates</artifactId>
        <version>3.0.7</version>
    </dependency>

2. ViewResolver ์„ค์ •

spring.groovy.template.prefix=/templates/
spring.groovy.template.suffix=.tpl


resources/templates๋ฅผ ๊ธฐ๋ณธ ๊ฒฝ๋กœ๋กœ ์„ค์ • ํ›„ ํ™•์žฅ์ž๋ฅผ .tpl๋กœ ์„ค์ •ํ•œ๋‹ค.

3. ๋ฉ”์†Œ๋“œ ์ž‘์„ฑ ๋ฐ ํ…œํ”Œ๋ฆฟ ์ž‘์„ฑ




๋ณ€์ˆ˜ ์ฐธ์กฐ๋Š” ${๋ณ€์ˆ˜๋ช…} ์œผ๋กœ ๋ณ€์ˆ˜ ์ฐธ์กฐํ•˜๋ฉฐ ${age > 18 ? '์„ฑ์ธ' : '๋ฏธ์„ฑ๋…„์ž'} ์™€ ๊ฐ™์€ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.
๊ธฐ๋ณธ ๋ฌธ๋ฒ•

  • ๋ณ€์ˆ˜ ์ฐธ์กฐ : ${๋ณ€์ˆ˜๋ช…}
  • ํ‘œํ˜„์‹ : ${ํ‘œํ˜„์‹} ex) ${์กฐ๊ฑด ? '์ฐธ':'๊ฑฐ์ง“'}
  • ๋ฐ˜๋ณต๋ฌธ : <% for (์š”์†Œ : ์ปฌ๋ ‰์…˜) { %> ๋ฐ˜๋ณต๋  ๋‚ด์šฉ <% } %>
<% for (item in items) { %>
    <li>${item}</li>
<% } %>
  • ์กฐ๊ฑด๋ฌธ : <% if (์กฐ๊ฑด) { %> ๋‚˜ํƒ€๋‚ผ ๊ฐ’ <% } %>
<% if (age >= 18) { %>
    <p>์„ฑ์ธ</p>
<% } else { %>
    <p>๋ฏธ์„ฑ๋…„์ž</p>
<% } %>
  • ์ธํด๋ฃจ๋“œ : header.tpl๊ณผ footer.tpl ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ˜„์žฌ ํ…œํ”Œ๋ฆฟ์— ์ธํด๋ฃจ๋“œ ํ•œ๋‹ค.
<% include "header.tpl" %>
<h1>Welcome!</h1>
<% include "footer.tpl" %>

๋งˆ๋ฌด๋ฆฌ

Spring Initializr์—์„œ ์ ์šฉ๊ฐ€๋Šฅํ•œ ๋Œ€ํ‘œ์ ์ธ ํ…œํ”Œ๋ฆฟ ์—”์ง„ 4๊ฐ€์ง€์˜ ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. jsp์™€ ๊ฐ™์€ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋‹ค๋ฅธ ํ…œํ”Œ๋ฆฟ ์—”์ง„๋„ ์žˆ์ง€๋งŒ jsp์˜ ๊ฒฝ์šฐ SpringBoot์—์„œ๋Š” ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜์ง€ ์•Š๊ธฐ(์„ค์ •ํ•œ๋‹ค๋ฉด ์‚ฌ์šฉ์€ ๊ฐ€๋Šฅํ•˜๋‹ค.) ๋•Œ๋ฌธ์— ๊ณต์‹์ ์œผ๋กœ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ํ™•์ธํ•ด๋ดค๋‹ค. 4๊ฐœ์˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š”๊ฒƒ์€ Thymeleaf๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋˜์ง€๋งŒ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹์„ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

profile
๋„๋ฆฌ๋„๋ฆฌ๋‹ด๋‹ด ๐Ÿค๐Ÿฅ๐Ÿค๐Ÿฅ๐Ÿฃ๐Ÿฃ

0๊ฐœ์˜ ๋Œ“๊ธ€