[Spring Boot] ํ์๋ฆฌํ ์์ , Model ๊ฐ์ฒด๋?
@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 ์ฐพ์ผ๋ฌ ๊ฐ๊ฒ์ง~~
โ
์ปจํธ๋กค๋ฌ๋ ๋ชจ๋ธ์ ํตํด์ ๋ฐ์ดํฐ ์ค์ , ๋ชจ๋ธ์ ๋ทฐ๋ก ์ด ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ,
ํค์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ๋ทฐ์ ๋ฟ๋ฆผ
โ