[ Spring ] @ModelAttribute Annotation
2024. 2. 15. 23:27ㆍ· BACK-END/└ Spring Boot
환경 : Spring Tool Suite4
Model 객체
- Controller에서 생성된 데이터를 담아 View로 전달할 때 사용하는 객체
@ModelAttribute
- @ModelAttribute 어노테이션은 스프링 MVC에서 컨트롤러 메서드에서 사용되며, 모델에 속성을 추가하는 데 사용됩니다.
- HTTP Body 내용과 HTTP 파라미터 값들을 Getter/Setter/생성자를 통해 주입하기 위해 사용
- 주로 DTO(Data Transfer Object) 객체를 가져올 때 사용
[ @ModelAttribute 사용 예 ]
@RestController//등록
@RequestMapping("/menu")//공용주소
public class MenuController {
//주세요
@Autowired
private MenuDao dao;
@RequestMapping("/insert")
public String insert(@ModelAttribute MenuDto dto) {
dao.insert(dto);
return "메뉴 등록 완료";
}
}
(+) MenuDto 객체를 가져오는 예시를 일부 발췌해왔다.
(+) HTTP 파마리터로 받은 값을 Dto 객체로 받아와 DAO에 있는 insert() 메소드를 활용하여 데이터 베이스에 값들을 삽입하는 식이다.
개인 공부 기록용입니다:)
728x90
'· BACK-END > └ Spring Boot' 카테고리의 다른 글
[ Spring ] @RequestMapping Annotation (0) | 2024.02.17 |
---|---|
[ Spring ] @RequestParam Annotation (0) | 2024.02.17 |
[ Spring ] @Autowired Annotation (0) | 2024.02.14 |
[ Spring ] @Controller와 @RestController의 차이 (0) | 2024.02.12 |
[ Spring ] @Repository / @Service / @Controller (0) | 2024.02.11 |