forked from castello/spring_basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoilTellerMVC2.java
More file actions
54 lines (42 loc) · 1.75 KB
/
YoilTellerMVC2.java
File metadata and controls
54 lines (42 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.fastcampus.ch2;
import java.util.Calendar;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class YoilTellerMVC2 {
@ExceptionHandler(Exception.class)
public String catcher(Exception ex) {
return "yoilError";
}
@RequestMapping("/getYoilMVC2") // http://localhost/ch2/getYoilMVC2
public String main(@RequestParam(required=true) int year,
@RequestParam(required=true) int month,
@RequestParam(required=true) int day, Model model) {
// 1. 유효성 검사
if(!isValid(year, month, day))
return "yoilError"; // 유효하지 않으면, /WEB-INF/views/yoilError.jsp로 이동
// 2. 처리
char yoil = getYoil(year, month, day);
// 3. Model에 작업 결과 저장
model.addAttribute("year", year);
model.addAttribute("month", month);
model.addAttribute("day", day);
model.addAttribute("yoil", yoil);
// 4. 작업 결과를 보여줄 View의 이름을 반환
return "yoil"; // /WEB-INF/views/yoil.jsp
}
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);
}
private boolean isValid(int year, int month, int day) {
if(year==-1 || month==-1 || day==-1)
return false;
return (1<=month && month<=12) && (1<=day && day<=31); // 간단히 체크
}
}