본문 바로가기

Java & Spring/SpringFramework

18. @RequestMapping 21:56

728x90

2. @ModelAtrribute

- 적용 대상을 Model의 속성으로 자동 추가해주는 애너테이션

- 반환 타입 또는 컨트롤러 메서드의 매개변수에 적용 가능

 

//	public String main(@ModelAttribute("myDate") MyData date, Model model) //  아래와 동일
	public String main(@ModelAttribute MyDate date, Model model) throws IOException {

//		model.addAttribute("myDate", date); // 생략가능
//		model.addAttribute("yoil", yoil);

컨트롤러 매개변수에

@RequestParam : 기본형, String

@ModelAttribute : 참조형

애노테이션이 생략되어 있다.

 

3. WebDataBinder란?

	@RequestMapping("/getYoilMVC5")
	public String main(@ModelAttribute MyDate date, BindingResult result) {

http://localhost/ch2/getYoilMVC5?year=2021&month=10&day=1 호출

name value
"year" "2021"
"month" "10"
"day" "1"

 

1. 타입변환

String 일때 ------------> int

BindingResult에 에러나, 결과를 저장.

 

2. 데이터 검증

month는 1 ~ 12

day 1 ~ 31

데이터 검증을 통해 걸러야함.

BindingResult에 에러나, 결과를 저장.

 

3. 바인딩 한 결과를 BindingResultr 담아 Controller에 넘겨줌.

(@ModelAttribute MyDate date, BindingResult result) {

                                                    꼭 뒤에 와야함.

 

728x90

'Java & Spring > SpringFramework' 카테고리의 다른 글

17. @RequestParam과 @ModelAttribute 33:43  (0) 2022.08.01
16. 서블릿과 JSP(4) 21:36  (0) 2022.08.01
15. 서블릿과 JSP(3) 39:36  (0) 2022.08.01
14. 서블릿과 JSP(2) 25:09  (0) 2022.08.01
13. 서블릿과 JSP(1) 31:47  (0) 2022.08.01