- 클라이언트가 접속하면 디스패쳐 서블릿이 받아온다. (코딩 상에서는 눈에 보이지 않는다.)
(우리의 목적은 컨트롤러에 있는 함수 실행. URL을 통해 VIEW를 결정한다.)- 디스패쳐 서블릿이 핸들러 맵핑을 찾아간다. (핸들러 맵핑은 컨트롤러를 찾는 역할을 한다.)
- 핸들러 어댑터를 통해 해당 컨트롤러의 맵핑에 맞는 함수를 찾는다.
- 컨트롤러를 실행하고 리턴한다. (Model and View 객체로 넘긴다)
HomeController.java
package edu.global.ex;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import lombok.extern.slf4j.Slf4j;
/**
* Handles requests for the application home page.
*/
@Slf4j
@Controller
public class HomeController {
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
log.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
@GetMapping("/hello")
public String hello(Model model) {
log.info("hello()...");
return "hello";
}
}
Hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>There is no place better than home.</h1>
<p>집 최고~~~!!</p>
</body>
</html>