최초 클라이언트로부터 요청이 들어왔을때 컨트롤러로 진입하게 됩니다.
그리고 컨트롤러는 요청에 대한 작업을 한 후 뷰쪽으로 데이터를 전합니다.
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.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";
}
HomeController 는 클라이언트가 요청을 보내면 dispatcher 가 controller 에게 보내서 요구사항에 맞는 view 를 보여지게 한다,
메소드 위에 어노테이션이 보인다, 프로젝트명/ 라고 url 을날리면 해당 메소드가 실행된다.
해당 메소드 의 모델에 serverTime 을 저장하고 home 문자열을 반환한다.
문자열은
servlet-context.xml 에 가서
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.javalec.ex" />
</beans:beans>
beans 가 가진 prefix 와 suffix 와 합쳐진다 -> home.jsp 로 간다
@RequestMapping("board/view")
public String view() {
return "board/view";
}
@RequestMapping("board/content")
public String content(Model model) {
return "board/content";
}
controller 에 해당 메소드를 만들었다, url에 /프로젝트명/board/view 라고 하면 return 값의 문자열에 .jsp 가 붙어 파일을 찾아 간다, 그럴러면 board 폴더가 있어야 겠다
@RequestMapping("/board/reply")
public ModelAndView reply() {
ModelAndView mv = new ModelAndView();
mv.addObject("id",30);
mv.setViewName("board/reply");
return mv;
}
reply 메소드 실행하면 mv 에 id 30 이랑 view name 을 세팅할 수 있다
package com.javalec.ex;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("/view/*")
public String view() {
return "view";
}
}
HomeController 옆에 두고 MyController 를 만든어 어노테이션을 줫다, 요청에 /view/* 어떤 값이 들어와서 return 은 view 이다. Controller 가 한 개일 필요가 없다는 의미.