2-10 관심사 분리, MVC패턴 - 실습

서현우·2022년 5월 23일
0

복습

목록 보기
9/34
package com.fastcampus.ch2;

import java.io.IOException;
import java.util.Calendar;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class YoilTellerMVC {
	@RequestMapping("/getYoilMVC")
	public ModelAndView main(int year, int month, int day) throws IOException {
		
		ModelAndView mv = new ModelAndView();
		
		char yoil = getYoil(year, month, day);
		
		mv.addObject("year", year);
		mv.addObject("month", month);
		mv.addObject("day", day);
		mv.addObject("yoil", yoil);
		
		mv.setViewName("yoil");
		
		return mv;
		//1. 유효성 검사
//		if(!isValid(year, month, day))
//			return "yoilError";
		
		//2. 요일 계산
		
		//3. 계산한 결과를 모델에 저장
//		model.addAttribute("year", year);
//		model.addAttribute("month", month);
//		model.addAttribute("day", day);
//		model.addAttribute("yoil", yoil);

//		return "yoil"; // /WEB-INF/views/yoil.jsp

	}

	private boolean isValid(int year, int month, int day) {
		// TODO Auto-generated method stub
		return true;
	}

	private char getYoil(int year, int month, int day) {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month-1, day);
		
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		return " 일월화수목금토".charAt(dayOfWeek);
	}
}

컨트롤러에서 맵핑된 메서드의 타입을 void로 하고 리턴값이 없으면,
맵핑된 주소의 이름을 가진 jsp이름을 리턴함.

Model을 매개변수로 받을 때는 DispatcherServlet에서 Model객체를 만들어서 컨트롤러로 넘겨주지만,
ModelAndView를 사용하면 컨트롤러에서 ModelAndView객체를 직접 만들어서,
유효성검사와 처리를 하고, 모델앤뷰 객체에 데이터를 저장하고 뷰 이름을 모델앤뷰에 저장해서 모델엔뷰객체를 리턴함.

profile
안녕하세요!!

0개의 댓글