Spring/Spring μž…λ¬Έ

μŠ€ν”„λ§ μž…λ¬Έ - (3) Welcome Page λ§Œλ“€κΈ° / View ν™˜κ²½μ„€μ •

hello_u 2023. 1. 9. 13:32


main/resources/static 에 idex.html 파일 생성 ν›„ μ½”λ“œ μž‘μ„±

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



<a href = " "> νƒœκ·Έμ™€ 속성은 λ‹€λ₯Έ μ›Ήμ‚¬μ΄νŠΈλ‘œ 링크λ₯Ό ν•˜λŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.
aλŠ” <a> νƒœκ·Έ μž…λ‹ˆλ‹€. μ™ΈλΆ€ λ¬Έμ„œλ‚˜ λ‚΄λΆ€ λ¬Έμ„œλ₯Ό 링크할 λ•Œ μ‚¬μš©ν•˜λŠ” νƒœκ·Έμž…λ‹ˆλ‹€.
hrefλŠ” hypertext reference의 μ•½μžλ‘œ μ‹€μ œλ‘œ 이동할 μ›ΉνŽ˜μ΄μ§€μ˜ μ£Όμ†Œλ₯Ό 뒀에 μ λŠ” μ†μ„±μž…λ‹ˆλ‹€.







μ„œλ²„λ₯Ό 껏닀가 λ‹€μ‹œ ν‚€κ³ 
localhost:8080 접속

Welcome Page 생성 μ™„λ£Œ



ν•„μš”ν•œκ²ƒμ„ μ°ΎλŠ” λŠ₯λ ₯

https://spring.io/projects/spring-boot

Spring Boot

Commercial support Business support from Spring experts during the OSS timeline, plus extended support after OSS End-Of-Life. Publicly available releases for critical bugfixes and security issues when requested by customers.

spring.io


Learn - Reference Doc λ“€μ–΄κ°€μ„œ 찾고싢은 ν‚€μ›Œλ“œ 검색

index.html νŒŒμΌμ„ static μ—μ„œ λ¨Όμ € μ°Ύκ³  , μ°Ύμ§€ λͺ»ν•˜λ©΄
index νƒ¬ν”Œλ¦Ώμ„ μ°ΎλŠ”λ‹€

방금 ν•œκ²ƒμ€
μ •μ νŽ˜μ΄μ§€ : λ‚΄κ°€ μž‘μ„±ν•œ μ†ŒμŠ€μ½”λ“œλ₯Ό μ›Ή λΈŒλΌμš°μ €λ‘œ κ·ΈλŒ€λ‘œ λ„˜κ²¨μ£ΌλŠ” 것 / ν”„λ‘œκ·Έλž˜λ°μ΄ μ•„λ‹˜


thymeleaf ν…œν”Œλ¦Ώ μ—”μ§„
thymeleaf 곡식 μ‚¬μ΄νŠΈ: https://www.thymeleaf.org/
μŠ€ν”„λ§ 곡식 νŠœν† λ¦¬μ–Ό: https://spring.io/guides/gs/serving-web-content/
μŠ€ν”„λ§λΆ€νŠΈ 메뉴얼: https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-template-engines

07-01 νŒ¨ν‚€μ§€ (Package)

https://wikidocs.net/231

07-01 νŒ¨ν‚€μ§€ (Package)

μ—¬λŸ¬λΆ„μ€ μ•„λ§ˆλ„ 폴더λ₯Ό μ΄μš©ν•˜μ—¬ νŒŒμΌμ„ 정리해 λ³Έ κ²½ν—˜μ΄ μžˆμ„κ²ƒμ΄λ‹€. 파일 μ—¬λŸ¬κ°œκ°€ ν•œ 폴더에 λ’€μ£½λ°•μ£½ μ„žμ—¬ μžˆμ„ λ•Œ νŒŒμΌλ“€μ„ 성격에 맞게 λΆ„λ₯˜ν•œ λ‹€μŒ μƒˆ 폴더에 μ €μž₯ν•˜λŠ” μ‹μœΌλ‘œβ€¦

wikidocs.net

μ—¬λŸ¬λΆ„μ€ μ•„λ§ˆλ„ 폴더λ₯Ό μ΄μš©ν•˜μ—¬ νŒŒμΌμ„ 정리해 λ³Έ κ²½ν—˜μ΄ μžˆμ„κ²ƒμ΄λ‹€. 파일 μ—¬λŸ¬κ°œκ°€ ν•œ 폴더에 λ’€μ£½λ°•μ£½ μ„žμ—¬ μžˆμ„ λ•Œ νŒŒμΌλ“€μ„ 성격에 맞게 λΆ„λ₯˜ν•œ λ‹€μŒ μƒˆ 폴더에 μ €μž₯ν•˜λŠ” μ‹μœΌλ‘œ 말이닀.

μžλ°”μ˜ νŒ¨ν‚€μ§€λ„ 이와 μ™„μ „νžˆ λ™μΌν•œ κ°œλ…μ΄λΌ ν• μˆ˜ μžˆλ‹€. νŒ¨ν‚€μ§€λŠ” λΉ„μŠ·ν•œ μ„±κ²©μ˜ μžλ°” ν΄λž˜μŠ€λ“€μ„ λͺ¨μ•„ 놓은 μžλ°”μ˜ 디렉토리이닀.

 μ˜ˆλ₯Ό λ“€μ–΄ HousePark, HouseKim λ“±μ˜ ν΄λž˜μŠ€λ“€μ€ houseλΌλŠ” νŒ¨ν‚€μ§€λ‘œ λΆ„λ₯˜ν•˜λ©΄ νŽΈλ¦¬ν•  것이닀


05-02 클래슀

https://wikidocs.net/214

05-02 클래슀

이번 μž₯μ—μ„œλŠ” ν΄λž˜μŠ€μ— λŒ€ν•΄μ„œ μžμ„Ένžˆ μ•Œμ•„λ³΄μž. μ—¬κΈ°μ„œλŠ” ν΄λž˜μŠ€μ— λŒ€ν•œ κ°œλ…μ μΈ μ„€λͺ…을 ν•˜κΈ°λ³΄λ‹€λŠ” κ°€μž₯ κ°„λ‹¨ν•œ 클래슀λ₯Ό μž‘μ„±ν•˜λŠ” κ²ƒμ—μ„œλΆ€ν„° μ‹œμž‘ν•˜μ—¬ μ‘°κΈˆμ”© 클래슀의 규λͺ¨λ₯Ό 킀…

wikidocs.net

κ·Έ κΈ°λŠ₯은 λ°”λ‘œ 객체(object)λ₯Ό λ§Œλ“œλŠ” κΈ°λŠ₯이닀.

객체와 μΈμŠ€ν„΄μŠ€
ν΄λž˜μŠ€μ— μ˜ν•΄μ„œ λ§Œλ“€μ–΄μ§„ 객체λ₯Ό μΈμŠ€ν„΄μŠ€λΌκ³ λ„ ν•œλ‹€. κ·Έλ ‡λ‹€λ©΄ 객체와 μΈμŠ€ν„΄μŠ€μ˜ μ°¨μ΄λŠ” λ¬΄μ—‡μΌκΉŒ?
μ΄λ ‡κ²Œ 생각 ν•΄ 보자. Animal cat = new Animal() μ΄λ ‡κ²Œ λ§Œλ“€μ–΄μ§„ cat은 객체이닀. 그리고 catμ΄λΌλŠ” κ°μ²΄λŠ” Animal의 μΈμŠ€ν„΄μŠ€(instance) 이닀. μΈμŠ€ν„΄μŠ€λΌλŠ” 말은 νŠΉμ • 객체(cat)κ°€ μ–΄λ–€ 클래슀(Animal)의 객체인지λ₯Ό κ΄€κ³„μœ„μ£Όλ‘œ μ„€λͺ…ν•  λ•Œ μ‚¬μš©λœλ‹€. 즉, "cat은 μΈμŠ€ν„΄μŠ€" λ³΄λ‹€λŠ” "cat은 객체"λΌλŠ” ν‘œν˜„μ΄ "cat은 Animal의 객체" λ³΄λ‹€λŠ” "cat은 Animal의 μΈμŠ€ν„΄μŠ€" λΌλŠ” ν‘œν˜„μ΄ 훨씬 잘 μ–΄μšΈλ¦°λ‹€.


Controller λž€ 무엇인가?
μ›Ή μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ 첫번째 μ§„μž…μ 
1. μ‚¬μš©μžμ˜ μš”μ²­μ΄ μ§„μž…ν•˜λŠ” 지점(entry point)



hello.hellospring.controller νŒ¨ν‚€μ§€ 생성
HelloContrller 클래슀 생성





package hello.hellospring.controller;

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

@Controller
public class HelloController {

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

    }
}




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}" >μ•ˆλ…•ν•˜μ„Έμš”. μ†λ‹˜</p>
</body>
</html>

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

 

th : thymeleaf νƒ¬ν”Œλ¦Ώ 문법 ,

<p th:text="'μ•ˆλ…•ν•˜μ„Έμš”. ' + ${data}" >μ•ˆλ…•ν•˜μ„Έμš”. μ†λ‹˜</p>
model.addAttribute("data","hello!!");

value( μ—¬κΈ°μ„œλŠ” "hello!!" ) κ°€ ${data} μΉ˜ν™˜


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

http GET 방식 , μ›Ή μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ URL 에 /hello 라고 λ“€μ–΄μ˜€λ©΄(맀칭이 되면) 이 λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•œλ‹€

Model model
μŠ€ν”„λ§μ΄ λͺ¨λΈμ„ λ§Œλ“€μ–΄ λ„£μ–΄μ€€λ‹€

return "hello";
--> resources/templates 에 μžˆλŠ” hello.html μ—κ°€μ„œ λ Œλ”λ§ν•΄λΌ , 이 화면을 μ‹€ν–‰μ‹œμΌœλΌ , λͺ¨λΈμ„ λ„˜κΈ°λ©΄μ„œ

resources:templates/hello.html
μ—¬κΈ°μ„œλŠ” hello κ°€ {ViewName}