๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring Boot

[Spring Boot] ํƒ€์ž„๋ฆฌํ”„ ์˜ˆ์ œ, Model ๊ฐ์ฒด๋ž€?

by ์šฐ์ฃผ๋ฌผ๊ณ ๊ธฐ 2025. 5. 3.
๋ฐ˜์‘ํ˜•
@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 ์ฐพ์œผ๋Ÿฌ ๊ฐ€๊ฒŸ์ง€~~

โ€‹

์ปจํŠธ๋กค๋Ÿฌ๋Š” ๋ชจ๋ธ์„ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ ์„ค์ •, ๋ชจ๋ธ์€ ๋ทฐ๋กœ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ,

ํ‚ค์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ์— ๋ฟŒ๋ฆผ

โ€‹

 

 

๋ฐ˜์‘ํ˜•