MCPcopy Create free account
hub / github.com/castello/spring_basic / YoilTellerMVC5

Class YoilTellerMVC5

ch2/YoilTellerMVC5.java:10–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import org.springframework.web.bind.annotation.RequestMapping;
9
10@Controller
11public class YoilTellerMVC5 {
12 @ExceptionHandler(Exception.class)
13 public String catcher(Exception ex) {
14 System.out.println("ex="+ex);
15
16 return "yoilError";
17 }
18
19 @RequestMapping("/getYoilMVC5") // http://localhost/ch2/getYoilMVC5?year=2021&month=10&day=1
20// public String main(@ModelAttribute("myDate") MyDate date, Model m) { // 아래와 동일
21 public String main(@ModelAttribute MyDate date, Model m) { // @ModelAttribute사용, 반환 타입은 String
22System.out.println("myDate="+date);
23
24 // 1. 유효성 검사
25 if(!isValid(date))
26 return "yoilError";
27
28 // 2. 처리
29 char yoil = getYoil(date);
30
31 // 3. Model에 작업한 결과를 저장
32 // @ModelAttribute 덕분에 MyDate를 저장안해도 됨. View로 자동 전달됨.
33// m.addAttribute("myDate", date);
34// m.addAttribute("yoil", yoil);
35
36 // 4. 작업 결과를 보여줄 뷰의 이름을 반환
37 return "yoil";
38 }
39
40 private @ModelAttribute("yoil") char getYoil(MyDate date) {
41 return getYoil(date.getYear(), date.getMonth(), date.getDay());
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(MyDate date) {
53 return isValid(date.getYear(), date.getMonth(), date.getDay());
54 }
55
56 private boolean isValid(int year, int month, int day) {
57 if(year==-1 || month==-1 || day==-1)
58 return false;
59
60 return (1<=month && month<=12) && (1<=day && day<=31); // 간단히 체크
61 }
62}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected