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