MVC 패턴
@Controller
public class YoilTeller {
@RequestMapping("/getYoil")
public void main(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 1. 입력 작업
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
int yyyy = Integer.parseInt(year);
int mm = Integer.parseInt(month);
int dd = Integer.parseInt(day);
//매개변수 request(객체를 가르키고 있는)안에 있는 값을 getParameter로 꺼내서 사용
---------------------------------------//After
@Controller
public class YoilTeller {
@RequestMapping("/getYoil")
public String main(int year, int month, int day, Model.model) throws IOException {
// 개별 매개변수로 받도록 변경, 값을 바로 사용가능
@Controller
public class YoilTeller {
@RequestMapping("/getYoil")
public void main(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 1. 입력
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
int yyyy = Integer.parseInt(year);
int mm = Integer.parseInt(month);
int dd = Integer.parseInt(day);
// 2. 처리
Calendar cal = Calendar.getInstance();
cal.set(yyyy, mm - 1, dd);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
char yoil = " 일월화수목금토".charAt(dayOfWeek);
----------------------------------------//After
@Controller
public class YoilTeller {
@RequestMapping("/getYoil")
public String main(int year, int month, int day, Model.model) throws IOException {
//1. 유효성 검사
if(!isValid(year, month, day))
return "yoilError";
// 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);
return "yoil"; //WEB - INF/views/yoil.jsp
// yoil(결과를 보여줄 View 이름)을 반환
// 모델은 현재Controller 전에 DispatcherServlet에서 만들어진 모델이기 때문에 따로 반환이 필요없다.
// View이름을 반환하면 DispatcherServlet에서 yoil.jsp에 모델을 전달한다.
@Controller
public class YoilTeller {
@RequestMapping("/getYoil")
public void main(HttpServletRequest request, HttpServletResponse response) throws IOException {
.
.
.//(중간 생략)
// 3. 출력
// System.out.println(year + "년 " + month + "월 " + day + "일은 ");
// System.out.println(yoil + "요일입니다.");
response.setContentType("text/html"); // 응답의 형식을 html로 지정
response.setCharacterEncoding("utf-8"); // 응답의 인코딩을 utf-8로 지정
PrintWriter out = response.getWriter(); // 브라우저로의 출력 스트림(out)을 얻는다.
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println(year + "년 " + month + "월 " + day + "일은 ");
out.println(yoil + "요일입니다.");
out.println("</body>");
out.println("</html>");
out.close();
// 한곳에 입력,처리,출력을 만들어놓음
------------------------------------//After
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>${year}년 ${month}월 ${day}일은 ${yoil}요일 입니다.</h1>
</body>
</html>
//(views)yoil.jsp를 따로 만들어줘서 분리