Eclipse4.2(JUNO) + STS PlugIn을 이용한 Spring MVC HelloWorld 작성하기
- STS(SpringSource tools Suite)와 maven Eclipse PlugIn 설치를 마쳤으니 Spring MVC를 이용하여 간단한 HelloWorld를 만들어 보자. 물론 Tomcat은 설치되어 있어야 하는데 이전 강좌의 내용을 참고 바란다.
- Spring Template Project를 이용하면 템플릿 코드를 다 만들어 주니 참으로 간단하다.
1. 이클립스 상단 왼쪽 File >> New 클릭 >> Spring Template Project 클릭
2. Spring MVC Project 클릭 후 Next
3. Project name과 top level 패키지명을 기술하는데 아래의 경우 helloworld가 app의 이름이 된다. 즉 나중에 실행시 브라우저에서 http://localhost:8080/helloworld 라고 하는 것이다. 입력 후 Next를 클릭
4. 이제 이클립스에 SprignHello 라는 프로젝트가 생긴 것을 확인 할 수 있을 것이다.
5. 실행해 보자.
Finish 클릭
참 간단하다.
이제부터는 템플릿으로 받은 소스에 대해 분석해 보도록 하자.
[WEB-INF/web.xml]
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
web.xml 파일은 웹애플리케이션이 처음 로딩될 때 실행되는 것으로 아주 중요한 파일이다. listener 태그안에 있는 ContextLoaderListener는 웹애플레케이션의 컨텍스트를 생성하는데 DisplatcherServlet이 이러한 역할 을 할수도 있다.
“/”요청이 들어오면 “appServlet”이 처리하는데 이파일은 실제 springframework.web.servlet.DispatcherServlet 파일이며 읽어 올 설정파일은 /WEB-INF/spring/appServlet/servlet-context.xml 이다.
ContextLoaderListener와 DisplatcherServlet은 둘다 웹애플리케이션 컨텍스트를 생성하지만 ContextLoaderListener가 ROOT 컨텍스트이면 DisplatcherServlet은 자식 컨텍스트이다.
(보다 세밀한 내용은 ContextLoaderListener, DisplatcherServlet 강좌를 참고하라)
브라우저에서 http://locahost:8080/helloworld/ 라는 요청을 보내면 DispatcherServlet이 어떤 컨트롤러가 처리할 지를 servlet-context.xml 파일을 읽어 아는 것이다.
그럼 servlet-context.xml 파일을 확인 해 보자.
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources
@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";
}
}
클래스 정의 윗부분 @Controller 애노테이션 때문에 이 클래스가 컨트롤러가 되는 것이며 @RequestMapping(value = "/",…) 부분에 의해 “/”요청이 들어오면 home method가 실행되는 것이다.
마지막 return “home”을 하게 되면 다시 이 클래스를 처음 요청한 DispatcherServlet이 받는데 디스패처서블릿은 뷰리졸버 한테 정의된 클래스에게 “home”이라는 뷰 이름을 해석시키는데 뷰리졸버는 prefix, suffix를 붙여 뷰 이름을 “/WEB-INF/view/hello.jsp”로 해석하여 해당 JSP가 실행 되는 것이다.
다음은 home.jsp의 모양이다.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
댓글 없음:
댓글 쓰기