@Controller
public class ExampleController {
@GetMapping("/thymeleaf/example")
public String thymeleafExample(Model model) {
Person examplePerson = new Person();
examplePerson.setId(1L);
examplePerson.setName("ํ๊ธธ๋");
examplePerson.setAge(18);
examplePerson.setHobbies(List.of("์ด๋", "๋
์"));
model.addAttribute("person", examplePerson);
model.addAttribute("today", LocalDate.now());
return "example";
}
}
@Getter
@Setter
public class Person {
private Long id;
private String name;
private int age;
private List<String> hobbies;
}
๊ฐ๋จํ ์ฝ๋.
โ
โ
Model ๊ฐ์ฒด๋, HTML์ชฝ์ผ๋ก ๊ฐ ๋๊ฒจ์ฃผ๋ ๊ฐ์ฒด์.
โ
addAttribute()๋ฉ์๋ ์จ์ ๋ชจ๋ธ์ ๊ฐ์ ์ ์ฅํจ
= model์๋ person, today ๋๊ฐ์ ํค๋ฅผ ๊ฐ์ง ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ์์.
โ
์์ ์ฝ๋์ ๊ฒฝ์ฐ,
person์ด๋ผ๋ ํค์ ์ฌ๋ ์ ๋ณด๋ฅผ
today๋ผ๋ ํค์ ๋ ์ง ์ ๋ณด๋ฅผ ์ ์ฅ.
โ
thymeleafExample()๋ฉ์๋๋ String์ผ๋ก "example"์ ๋ฐํํ๋๋ฐ,
โ
์๋ณด๋ฉด ์ด ์ปจํธ๋กค๋ฌ์๋ ์ด๋ ธํ ์ด์ ์ด
@RestController๊ฐ ์๋๋ผ
@Controller๊ฐ ๋ถ์ด์์
= ๋ทฐ ์ด๋ฆ์ ๋ฐํํ๋ค๋๊ฑฐ!!!
= ์คํ๋ง์ "example"์ด๋ผ๋ ์ด๋ฆ์ ๋ทฐ ํ์ผ ์ฐพ์๊ฐ.
= example.html ์ฐพ์ผ๋ฌ ๊ฐ๊ฒ์ง~~
โ
์ปจํธ๋กค๋ฌ๋ ๋ชจ๋ธ์ ํตํด์ ๋ฐ์ดํฐ ์ค์ , ๋ชจ๋ธ์ ๋ทฐ๋ก ์ด ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ,
ํค์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ๋ทฐ์ ๋ฟ๋ฆผ
โ
'Spring Boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring Boot] ์ฟ ํค๋? Cookie ๐ช (0) | 2025.05.07 |
|---|---|
| [OAuth] OAuth๋? ํ๋ฆ, ์ฉ์ด ์ ๋ฆฌ (2) | 2025.05.07 |
| [Spring Boot] ํ์๋ฆฌํ ํํ์, ๋ฌธ๋ฒ ์ ๋ฆฌ (0) | 2025.05.03 |
| [Spring Boot] ํ์๋ฆฌํ ํ ํ๋ฆฟ ์์ง์ด๋?์์กด์ฑ ์ถ๊ฐ (0) | 2025.05.03 |
| [Spring Boot] @PathVariable vs. @RequestParam ๋ญ ์จ์ผํ ๊น? (feat.@QueryParam) (0) | 2025.05.03 |