[Spring Boot] thymeleaf์™€ Spring Boot

2021. 8. 4. 20:42ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”? ์ฃผ๋‹ˆํ•˜๋ž‘ ์ž…๋‹ˆ๋‹ค.

์ฃผ๋‹ˆํ•˜๋ž‘์€ Spring Boot์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

์ธํ”„๋Ÿฐ์˜ ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง ์ž…๋ฌธ - ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ํ”ผ๋“œ๋ฅผ ์ž‘์„ฑ ํ•ด ๋ณด๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

๋ฐ”๋กœ ์‹œ์ž‘ ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

 


 

 

๐Ÿ“‹ ๋ชฉ์ฐจ


01.[Spring Boot] thymeleaf์™€ Spring Boot
02.[Spring Boot] Spring Web ๊ฐœ๋ฐœ ๊ธฐ์ดˆ
03.[Spring Boot] ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - Backend
04.[Spring Boot] Service ๊ฐœ๋ฐœ ๋ฐ Test Case ์ž‘์„ฑ
05.[Spring Boot] Spring Bean๊ณผ ์˜์กด๊ด€๊ณ„
06.[Spring Boot] Java Code๋กœ ์ง์ ‘ Spring Bean ๋“ฑ๋ก
07.[Spring Boot] ๋“ฑ๋ก, ๋ชฉ๋ก ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ
08.[Spring Boot] ๊ธฐ์กด ์ฝ”๋“œ ์† ์•ˆ๋Œ€๊ณ , ์„ค์ •์œผ๋กœ ๊ตฌํ˜„ Class ๋ณ€๊ฒฝ
09.[Spring Boot] ํ†ตํ•ฉ Test
10.[Spring Boot] JPA
11.[Spring Boot] Spring Data JPA
12.[Spring Boot] AOP

 

 


 

๐Ÿ“Œ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—


์ฝ”๋“œ์— ๊ด€๋ จํ•œ ๋‚ด์šฉ์€ ์ฃผ๋‹ˆํ•˜๋ž‘์˜ Github์—์„œ ํ™•์ธ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

 

 

๐Ÿ“Œ thymeleaf


๐Ÿ“ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋ž€?

 

ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋ž€ ๋™์  ์ปจํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ ๊ฒƒ์ด์—์š”.

ํ…œํ”Œ๋ฆฟ ์–‘์‹๊ณผ ํŠน์ • ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์— ๋”ฐ๋ฅธ ์ž…๋ ฅ ์ž๋ฃŒ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ๊ฒฐ๊ณผ ๋ฌธ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๋งํ•˜๋ฉฐ view code(html)์™€ data logic code(db connection)์„ ๋ถ„๋ฆฌํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์Šคํ”„๋ง MVC์—์„œ ์ฃผ๋กœ ๋™์ ์ธ View๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  View๋งŒ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์œผ๋ฉฐ ์ด๋ฉ”์ผ ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค.

 

 

ํ…œํ”Œ๋ฆฟ ์—”์ง„์—๋Š” ์„œ๋ฒ„์‚ฌ์ด๋“œ, ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ต๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ํ…œํ”Œ๋ฆฟ ์—”์ง„ : ์„œ๋ฒ„์—์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ ์ •์˜๋œ ํ…œํ”Œ๋ฆฟ์— ๋„ฃ์–ด html์„ ๊ทธ๋ฆฐ ๋’ค ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ.
    • HTML ์ฝ”๋“œ์—์„œ ๊ณ ์ •์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ถ€๋ถ„์€ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ๋งŒ๋“ค์–ด๋‘๊ณ , ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ๋ถ€๋ถ„๋งŒ ํ…œํ”Œ๋ฆฟ์— ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ผ์›Œ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

  • ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ํ…œํ”Œ๋ฆฟ ์—”์ง„ : HTML ํ˜•ํƒœ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉฐ, ๋™์ ์œผ๋กœ DOM์„ ๊ทธ๋ฆฌ๊ฒŒ ํ•ด์ฃผ๋Š” ์—ญํ• .
    • ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ DOM ๊ฐ์ฒด์— ๋™์ ์œผ๋กœ ๊ทธ๋ ค์ฃผ๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

 

 

์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ž๋™ ์„ค์ •์„ ์ง€์›ํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„

    • FreeMarker
    • Groovy
    • Thymeleaf
    • Mustache



์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” JSP๋ฅผ ์‹ซ์–ดํ•ด์š”!

Thymeleaf์™€ ๋‹ฌ๋ฆฌ JSP๋Š” ์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ์ž๋™ ์„ค์ •์„ ์ง€์›ํ•˜์ง€ ์•Š์œผ๋ฉฐ ๊ถŒ์žฅํ•˜์ง€๋„ ์•Š๋Š”๋‹ต๋‹ˆ๋‹ค.
์™œ Spring Boot์—์„œ๋Š” JSP๋ฅผ ๊ถŒ์žฅํ•˜์ง€ ์•Š์„๊นŒ์š”?
์šฐ์„  JSP๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด JAR ํŒจํ‚ค์ง•์„ ํ•  ์ˆ˜ ์—†์–ด WAR ํŒจํ‚ค์ง•์„ ํ•ด์•ผํ•œ๋‹ค. ๋ฌผ๋ก , WARํŒจํ‚ค์ง•์œผ๋กœ๋„ ์ž„๋ฒ ๋””๋“œ ํ†ฐ์บฃ์œผ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฐฐํฌ๋„ ๊ฐ€๋Šฅํ•˜๊ธด ํ•ฉ๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ Undertow๋ผ๋Š” ์ตœ๊ทผ์— ๋งŒ๋“ค์–ด์ง„ ์„œ๋ธ”๋ฆฟ ์—”์ง„์ด JSP๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๋“ฑ ์ œ์•ฝ์‚ฌํ•ญ์ด ์žˆ๋‹ต๋‹ˆ๋‹ค.
๋˜ํ•œ JSP์— ๋Œ€ํ•œ ์˜์กด์„ฑ์„ ๋„ฃ์œผ๋ฉด ์˜์กด์„ฑ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.


 

JAR์™€ WAR๋Š” ๋ญ์—์š”?

JAR์™€ WAR ๋ชจ๋‘ JAVA์˜ jar ํˆด์„ ์ด์šฉํ•ด ์ƒ์„ฑ๋œ ํŒŒ์ผ์ด๋ฉฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‰ฝ๊ฒŒ ๋ฐฐํฌํ•˜๊ณ  ๋™์ž‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ๊ด€๋ จ ํŒŒ์ผ์„ ํŒจํ‚ค์ง•ํ•ด์ค€๋‹ต๋‹ˆ๋‹ค.

  • JAR (Java Archive)
    • .jar ์ž๋ฐ” ํ”„๋กœ์ ํŠธ๋ฅผ ์••์ถ•ํ•œ ํŒŒ์ผ.
    • ์ž๋ฐ” ๋ฆฌ์†Œ์Šค, ์†์„ฑํŒŒ์ผ, ๋ผ์ด๋ธŒ๋ฒ„๋ฆฌ ๋“ฑ์ด ํฌํ•จ.
    • ์›ํ•˜๋Š” ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ JDK(Java Development Kit)์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” JRE(Java Runtime Environment)๋งŒ ๊ฐ€์ง€๊ณ ๋„ ์‹คํ–‰ ๊ฐ€๋Šฅ.

  • WAR(Web Application Archive)
    • .war servlet / jsp ์ปจํ…Œ์ด๋„ˆ์— ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์••์ถ• ํŒŒ์ผ.
    • ์›น ๊ด€๋ จ ์ž์›๋งŒ์„ ํฌํ•จ (JSP, Servlet, JAR, Class, HTML ๋“ฑ).
    • JAR๊ณผ ๋‹ฌ๋ฆฌ WEB-INF ๋ฐ META-INF ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์‚ฌ์ „ ์ •์˜๋œ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ Tomcat๊ณผ ๊ฐ™์€ ์›น ์„œ๋ฒ„ ๋˜๋Š” ์›น ์ปจํ…Œ์ด๋„ˆ(WAS)๊ฐ€ ํ•„์š”.
    • WAR๋„ JAVA์˜ jar ์˜ต์…˜(java -jar)์„ ์ด์šฉํ•ด ์ƒ์„ฑํ•˜๋Š” JAR ํŒŒ์ผ์˜ ์ผ์ข….

 


๊ทธ๋ž˜์„œ tymeleaf๊ฐ€ ๋ญ์—์š”?


Thymeleaf๋Š” ๋น„๊ต์  ์ตœ๊ทผ์— ๋งŒ๋“ค์–ด์ง„ ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด๋ฉฐ, ์„œ๋ฒ„์‚ฌ์ด๋“œ ์ž๋ฐ” ํ…œํ”Œ๋ฆฟ ์—”์ง„์˜ ํ•œ ์ข…๋ฅ˜์ธ ๊ฒƒ์ด์—์š”.
JSP์™€ Thymeleaf์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ JSP์™€ ๋‹ฌ๋ฆฌ Servlet Code๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š๋‹ค๋Š” ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ ๋ถ„๋ฆฌ๋˜์–ด ์˜ค๋กœ์ง€ View์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ต๋‹ˆ๋‹ค.

 

@Controller
public class HelloController {

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

 

<!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>

 

 

'์•ˆ๋…•ํ•˜์„ธ์š” ์†๋‹˜'์€ ์–ด๋””๊ฐ€๊ณ , '์•ˆ๋…•ํ•˜์„ธ์š”. hello!!'๊ฐ€ ์ถœ๋ ฅ ๋˜์—ˆ์„๊นŒ์š”? '์•ˆ๋…•ํ•˜์„ธ์š”. ์†๋‹˜'์€ ์ •์  ์ปจํ…์ธ  ์ฆ‰, html๋กœ๋งŒ ์ž‘์„ฑ์„ ํ–ˆ์„ ๋•Œ ๋ณด์ด๋Š” ๋ถ€๋ถ„ ์ด๊ณ , MVC์™€ Templat Engin์„ ์‚ฌ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ์ถœ๋ ฅ์ด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

์œ„์™€ ๊ฐ™์ด ์ž‘๋™ํ•˜๋Š” ์›๋ฆฌ๋ฅผ ๋ณด์ž๋ฉด ๋จผ์ € Web Browser๊ฐ€ uri๋กœ hello๋ฅผ http Method get์„ ํ†ตํ•ด ์ „๋‹ฌํ•˜๋ฉด ํ†ฐ์ผ“ ์„œ๋ฒ„๊ฐ€ ์ด๊ฒƒ์„ ๋งจ ์ฒ˜์Œ ๋ฐ›์•„ Spring Boot์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด์—์š”. Spring Boot๋Š” ์ „๋‹ฌ ๋ฐ›์€ ๋‚ด์šฉ์„ ๋ณด๊ณ , hello Controller์—๊ฒŒ ์ด ๋‚ด์šฉ์„ ์ „๋‹ฌ ํ•ด ์ฃผ๊ณ , Method์•ˆ์— ์žˆ๋Š” String ํ˜•์‹์˜ hello๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, model์„ ํ†ตํ•ด data:hello!!๊ฐ€ ์ž‘์„ฑ ๋˜์–ด ์žˆ์œผ๋‹ˆ ์ด ๋‚ด์šฉ์„ viewResolver์—๊ฒŒ ์ „๋‹ฌ์„ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”. viewResolver๋Š” uri๊ฐ€ hello์ธ ๊ฒƒ์ด ๋ฐ˜ํ™˜๊ฐ’๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๊ฒƒ์— ๋งž๋Š” ํ™”๋ฉด์ธ
'main/resources/templates/hello.html์„ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜์„ ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

  • Spring Boot Templat Engin Default viewName Mapping
  • resources: templates/ +{ViewName} + .html

 

๐Ÿ’ก ์ฐธ๊ณ 
spring-boot-devtools Library๋ฅผ ์ถ”๊ฐ€ ์‹œ html File์„ ์ปดํŒŒ์ผ๋งŒ ํ•ด์ฃผ๋ฉด ์„œ๋ฒ„ ์žฌ ์‹œ์ž‘ ์—†์ด View File ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅ.

InteliJ ์ปดํŒŒ์ผ ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ•: Mene build -> Recompile

 

 

 

 


์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค

728x90
๋ฐ˜์‘ํ˜•