| 8 | import org.springframework.web.servlet.ModelAndView; |
| 9 | |
| 10 | @Controller |
| 11 | public class YoilTellerMVC3 { |
| 12 | @RequestMapping("/getYoilMVC3") // http://localhost/ch2/getYoilMVC3 |
| 13 | public ModelAndView main( // 반환 타입이 ModelAndView |
| 14 | @RequestParam(defaultValue="-1") int year, |
| 15 | @RequestParam(defaultValue="-1") int month, |
| 16 | @RequestParam(defaultValue="-1") int day |
| 17 | ) { |
| 18 | |
| 19 | // 1. ModelAndView를 생성 |
| 20 | ModelAndView mv = new ModelAndView(); |
| 21 | |
| 22 | // 2. 유효성 검사 |
| 23 | if(!isValid(year, month, day)) { |
| 24 | mv.setViewName("yoilError"); // View의 이름을 지정 |
| 25 | return mv; |
| 26 | } |
| 27 | |
| 28 | // 3. 처리 |
| 29 | char yoil = getYoil(year, month, day); |
| 30 | |
| 31 | // 4. ModelAndView에 작업한 결과를 저장 |
| 32 | mv.addObject("year", year); |
| 33 | mv.addObject("month", month); |
| 34 | mv.addObject("day", day); |
| 35 | mv.addObject("yoil", yoil); |
| 36 | |
| 37 | // 5. 작업 결과를 보여줄 View의 이름을 지정 |
| 38 | mv.setViewName("yoil"); |
| 39 | |
| 40 | // 6. ModelAndView를 반환 |
| 41 | return mv; |
| 42 | } |
| 43 | |
| 44 | private char getYoil(int year, int month, int day) { |
| 45 | Calendar cal = Calendar.getInstance(); |
| 46 | cal.set(year, month - 1, day); |
| 47 | |
| 48 | int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); |
| 49 | return " 일월화수목금토".charAt(dayOfWeek); |
| 50 | } |
| 51 | |
| 52 | private boolean isValid(int year, int month, int day) { |
| 53 | if(year==-1 || month==-1 || day==-1) |
| 54 | return false; |
| 55 | |
| 56 | return (1<=month && month<=12) && (1<=day && day<=31); // 간단히 체크 |
| 57 | } |
| 58 | } |
nothing calls this directly
no outgoing calls
no test coverage detected