728x90
List 6.1 HelloController.java
package com.tistory; import java.util.Calendar; import org.springframework.sterotype.Controller import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping("/hello.do") { public ModelAndView hello() { ModelAndView mav = new ModelAndView(); mav.setViewName("hello"); mav.addObject("greeting", getGreeting()); return mav; } private String getGreeting() { int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); if (hour >= 0 && hour <= 10) { return "Good morning."; } else if (hour >= 12 && hour <= 15) { return "Good afternoon."; } else if (hour >= 18 && hour <= 22) { return "Good night"; } return "Hello"; } }
line 9의 @Controller annotation은 해당 class가 Spring MVC의 controller를 구현한 class라는 것을 지정한다.
DispatcherServlet은 Spring Container에서 controller 객체를 검색하기 때문에 Spring 설정 file에 controller를 bean으로 등록한다.
728x90