환경정보
- eclipse indigo
- java1.6
- spring3.0
- tomcat7
1. project 생성
- group id는 도메인으로 입력(본인취향대로)
- artifact id는 프로젝트명
2. 프로젝트 생성후 프로젝트 properties > project facets 변경
- convert to faceted from 클릭
- Dynamic Web Module 체크
- 하단에 노란 박스 Further configuration available 클릭
- content directory 를 webapps로 변경
- Gernerate web.xml deployment descriptor 체크
- 모두 OK후에 webapps 폴더를 src/main/으로 이동
아래처럼 변경
3. 프로젝트 생성후 프로젝트 properties > Deployment Assembly 변경
- add 클릭하여 java build path Entries "Next" 후 maven 고르고 Finish
- 기존에 Deploy Path "/"를 Romove하고 새로 추가
- 모두 추가되어 완료된 모습.
4. 여기서부터 소스 추가 및 수정
src/main/webapps/WEB-INF/spring 폴더 추가
src/main/webapps/WEB-INF/spring/root-context.xml 파일생성
root-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
</beans>
src/main/webapps/WEB-INF/spring/appServlet 폴더추가
src/main/webapps/WEB-INF/spring/appServlet/servlet-context.xml 파일생성
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-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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="www.beans9.com" />
</beans:beans>
src/main/webapps/WEB-INF/web.xml 파일 변경
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- 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-app>
src/main/java/www/beans9/com/HomeController.java 파일생성
HomeController.java
package www.beans9.com;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! the client locale is "+ locale.toString());
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
src/main/webapps/WEB-INF/views 폴더 생성
src/main/webapps/WEB-INF/views/home.jsp 파일 생성
home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${serverTime }
</body>
</html>
자 이제 was를 기동시켜 Spring MVC가 제대로 동작하는지 확인
이상없이 출력된다면 ^^ 기본 환경 구축 완료.
해당 프로젝트 실제 파일경로들 /
혹 해당파일이 필요하신분들을 위해 프로젝트 Achiev파일을 올려놓으니^^ 사용하셔도 됩니다.
작성자 : beans9 (http://beans9.tistory.com)
다른곳에 기재하실경우 작성자표시를 꼭 해주시기 바랍니다.