[Spring][혼곡] 3. ν”„λ‘œμ νŠΈ λ§Œλ“€μ–΄λ³΄κΈ° - View ν™˜κ²½ μ„€μ • (3)

2021. 7. 3. 19:20Β·πŸ“ Language/✏ JAVA

ν”„λ‘œμ νŠΈ λ§Œλ“€μ–΄λ³΄κΈ°

2. View ν™˜κ²½ μ„€μ •

ν˜„μž¬ ν”„λ‘œμ νŠΈλ₯Ό μ‹€ν–‰ν•˜λ©΄ μ—λŸ¬ νŽ˜μ΄μ§€λ§Œ λ‚˜νƒ€λ‚˜λŠ”λ°, 이 νŽ˜μ΄μ§€λ₯Ό λ°”κΏ”λ³Ό 것이닀.

κΈ°μ‘΄ μ›Ήμ‚¬μ΄νŠΈ

 

1) Welcome νŽ˜μ΄μ§€ λ§Œλ“€κΈ°

Welcome νŽ˜μ΄μ§€λž€ 도메인을 μž…λ ₯ν•˜κ³  듀어왔을 λ•Œ λ‚˜νƒ€λ‚˜λŠ” 첫 νŽ˜μ΄μ§€λ₯Ό λ§ν•œλ‹€.

μŠ€ν”„λ§ λΆ€νŠΈλŠ” resources/static ν•΄λ‹Ή 폴더 μœ„μΉ˜μ— index.htmlλ₯Ό μƒμ„±ν•˜λ©΄ ν•΄λ‹Ή νŒŒμΌμ„ Welcome νŽ˜μ΄μ§€λ‘œ μΈμ‹ν•œλ‹€.

resources/static/index.html

 

μ§€κΈˆ Welcome νŽ˜μ΄μ§€μ˜ μƒνƒœλŠ” μ •적 νŽ˜μ΄μ§€ μƒνƒœμΈλ°, html νŒŒμΌμ„ μ›Ήμ„œλ²„κ°€ 읽어간 ν›„ λ‹¨μ§€ λ³΄μ—¬μ£ΌκΈ°λ§Œ ν•˜κ³  μžˆλ‹€.

μ—¬κΈ°μ„œ ν…œν”Œλ¦Ώ 엔진을 μ‚¬μš©ν•˜μ—¬ κ°œλ°œμžκ°€ μ›ν•˜λŠ” λŒ€λ‘œ λͺ¨μ–‘을 λ³€κ²½ν•  수 μžˆλ‹€.

 

2) thymeleaf ν…œν”Œλ¦Ώ μ—”μ§„

λ™μž‘ 방식

μ›Ή λΈŒλΌμš°μ €μ—μ„œ μ–΄λ–€ νŽ˜μ΄μ§€λ₯Ό λ˜μ§€λ©΄ λ‚΄μž₯ ν†°μΊ£ μ„œλ²„κ°€ λ°›μ•„μ„œ μŠ€ν”„λ§ν•œν…Œ μ „λ‹¬ν•œλ‹€.

전달받은 μŠ€ν”„λ§μ€ λ§€μΉ­ κ°€λŠ₯ν•œ μ»¨νŠΈλ‘€λŸ¬λ₯Ό μ°Ύμ•„ ν•΄λ‹Ή λ©”μ„œλ“œλ₯Ό μ‹€ν–‰μ‹œν‚¨λ‹€. (@GetMapping)

 

μ΄λ•Œ, μŠ€ν”„λ§μ΄ λͺ¨λΈμ΄λž€ 것을 λ§Œλ“€μ–΄ ν•¨κ»˜ λ„£μ–΄μ£ΌλŠ”λ° (Model model)

이 λͺ¨λΈμ— μ—¬λŸ¬ μ• νŠΈλ¦¬λ·°νŠΈλ“€μ„ μΆ”κ°€ν•˜μ—¬ 뷰에 보일 수 μžˆλ„λ‘ ν•  수 μžˆλ‹€. (model.addAttribute)

 

그리고 μ»¨νŠΈλ‘€λŸ¬μ—μ„œ 리턴 κ°’μœΌλ‘œ 문자λ₯Ό λ°˜ν™˜ν•˜λ©΄ viewResolverκ°€ 화면을 μ°Ύμ•„μ„œ μ²˜λ¦¬ν•˜λŠ”λ°, (return "hello";)

ν…œν”Œλ¦Ώ μ—”μ§„μ˜ κΈ°λ³Έ viewName 맀칭은 'resources/templates/{ViewName}.html'이닀.

 

λ™μž‘ ν™˜κ²½

 

Controller

  • μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 첫 번째 μ§„μž…μ μ΄λ‹€.
  • 컨트둀러λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•΄ λ¨Όμ € 컨트둀러의 νŒ¨ν‚€μ§€μ™€ java 클래슀 νŒŒμΌμ„ λ§Œλ“€μ–΄μ•Ό ν•œλ‹€.
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 {

    @GetMapping("hello") // μ›Ή μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ '/hello'둜 λ“€μ–΄μ˜€λ©΄ 이 λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œλ‹€.
    public String hello(Model model) {
        model.addAttribute("data", "hello!!");
        return "hello"; // λ¬Έμžμ—΄ 값에 λ§žλŠ” html을 μ°Ύμ•„ 화면에 보여쀀닀.
    }
}

 

View

  • HelloController의 View이닀.
  • νƒ€μž„λ¦¬ν”„μ˜ 문법을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄ th둜 μ„ μ–Έλ˜μ–΄ μžˆλ‹€.
  • {data}λŠ” μ•žμ„œ Controllerμ—μ„œ μž…λ ₯ν•œ λͺ¨λΈ μ• νŠΈλ¦¬λ·°νŠΈμ˜ key κ°’μœΌλ‘œ μ‹€ν–‰ ν™”λ©΄μ—” value 값이 보이게 λœλ‹€.
<!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>

 

3) μ‹€ν–‰ ν™”λ©΄

http://localhost:8080/ 둜 λ“€μ–΄κ°€λ©΄ Welcome νŽ˜μ΄μ§€κ°€ 보인닀.

http://localhost:8080/

 

helloλ₯Ό ν΄λ¦­ν•˜μ—¬ λ“€μ–΄κ°€λ©΄ hello νŽ˜μ΄μ§€μ™€ {data}둜 μž…λ ₯ν–ˆλ˜ ν…μŠ€νŠΈκ°€ μ• νŠΈλ¦¬λ·°νŠΈμ˜ value κ°’μœΌλ‘œ λ‚˜μ˜€κ³  μžˆλ‹€.

http://localhost:8080/hello

 

λ§ˆμ§€λ§‰μœΌλ‘œ ν˜„μž¬ ν”„λ‘œμ νŠΈμ˜ νŒ¨ν‚€μ§€μ™€ 파일 κ΅¬μ‘°λŠ” μ•„λž˜μ™€ κ°™λ‹€.

src

μ €μž‘μžν‘œμ‹œ
'πŸ“ Language/✏ JAVA' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [Spring][혼곡] 4. μŠ€ν”„λ§ μ›Ή 개발 기초 - 정적 컨텐츠 (1)
  • [Spring][혼곡] 3. ν”„λ‘œμ νŠΈ λ§Œλ“€μ–΄λ³΄κΈ° - λΉŒλ“œ 및 μ‹€ν–‰ (4)
  • [Spring][혼곡] 3. ν”„λ‘œμ νŠΈ λ§Œλ“€μ–΄λ³΄κΈ° - κΈ°λ³Έ μ„€μ • (2)
  • [Spring][혼곡] 3. ν”„λ‘œμ νŠΈ λ§Œλ“€μ–΄λ³΄κΈ° - κΈ°λ³Έ μ„€μ • (1)
Blxxming
Blxxming
CS 지식과 κ³΅λΆ€ν•˜λ‹€ 배운 것, κ²½ν—˜ν•œ 것 등을 κΈ°λ‘ν•˜λŠ” λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.
  • Blxxming
    πŸ’‘λ²ˆλœ©πŸ’‘
    Blxxming
  • 전체
    였늘
    μ–΄μ œ
  • 곡지사항

    • Tech Interview
    • πŸ“š Tech (246)
      • πŸ“ Computer Science (96)
        • ✏ OS (12)
        • ✏ Network & Web (10)
        • ✏ Database (11)
        • ✏ Data Structure (6)
        • ✏ Algorithm (40)
        • ✏ Design Pattern (9)
        • ✏ Cloud Computing (3)
        • ✏ (5)
      • πŸ“ Language (73)
        • ✏ Language (6)
        • ✏ C & C++ (11)
        • ✏ C# (19)
        • ✏ JAVA (37)
      • πŸ“ Game (43)
        • ✏ Computer Graphics (2)
        • ✏ Unity (14)
        • ✏ Unreal (26)
        • ✏ (1)
      • πŸ“ Book (34)
        • ✏ Effective (3)
        • ✏ Game Server (16)
        • ✏ Clean Code (14)
        • ✏ (1)
  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.0
Blxxming
[Spring][혼곡] 3. ν”„λ‘œμ νŠΈ λ§Œλ“€μ–΄λ³΄κΈ° - View ν™˜κ²½ μ„€μ • (3)
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”