| 11 | import org.springframework.web.bind.annotation.RequestParam; |
| 12 | |
| 13 | @Controller |
| 14 | public class YoilTellerMVC6 { |
| 15 | @ExceptionHandler(Exception.class) |
| 16 | public String catcher(Exception ex, BindingResult result) { |
| 17 | System.out.println("result="+result); |
| 18 | System.out.println("error="+result.getFieldError()); |
| 19 | |
| 20 | return "yoilError"; |
| 21 | } |
| 22 | |
| 23 | @RequestMapping("/getYoilMVC6") // http://localhost/ch2/getYoilMVC6 |
| 24 | // public String main(@ModelAttribute("myDate") MyDate date, Model model) { |
| 25 | public String main(MyDate date, BindingResult result) { |
| 26 | System.out.println("result="+result); |
| 27 | // 1. 유효성 검사 |
| 28 | if(!isValid(date)) |
| 29 | return "yoilError"; // 유효하지 않으면, /WEB-INF/views/yoilError.jsp로 이동 |
| 30 | |
| 31 | // 2. 처리 |
| 32 | // char yoil = getYoil(date); |
| 33 | |
| 34 | // 3. Model에 작업 결과 저장 |
| 35 | // model.addAttribute("myDate", date); |
| 36 | // model.addAttribute("yoil", yoil); |
| 37 | |
| 38 | // 4. 작업 결과를 보여줄 View의 이름을 반환 |
| 39 | return "yoil"; // /WEB-INF/views/yoil.jsp |
| 40 | } |
| 41 | |
| 42 | |
| 43 | private @ModelAttribute("yoil") char getYoil(MyDate date) { |
| 44 | return getYoil(date.getYear(), date.getMonth(), date.getDay()); |
| 45 | } |
| 46 | |
| 47 | private char getYoil(int year, int month, int day) { |
| 48 | Calendar cal = Calendar.getInstance(); |
| 49 | cal.set(year, month - 1, day); |
| 50 | |
| 51 | int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); |
| 52 | return " 일월화수목금토".charAt(dayOfWeek); |
| 53 | } |
| 54 | |
| 55 | private boolean isValid(MyDate date) { |
| 56 | if(date.getYear()==-1 || date.getMonth()==-1 || date.getDay()==-1) |
| 57 | return false; |
| 58 | |
| 59 | return (1<=date.getMonth() && date.getMonth()<=12) && (1<=date.getDay() && date.getDay()<=31); // 간단히 체크 |
| 60 | } |
| 61 | } |
nothing calls this directly
no outgoing calls
no test coverage detected