yoursyun
Eclipse 에서 SpringMVC를 MAVEN 으로 개발 환경 셋팅 본문
Eclipse 에서 SpringMVC를 MAVEN 으로 개발 환경 셋팅
Eclipse 에서Maven Project 를 이용하여 Spring MVC 개발 환경을 만들어 보자.
Dynamic Web Project > Spring 환경 추가 > WebContents > lib 에 Spring lib 추가 등을 통해 작성 했었지만, 지난 포스트와 같이 Maven을 통하여 동일한 루트를 구현 하고자 한다.
1. Kepler 버전에서 ( JavaEE ) 을 설치하면 Spring, Maven 등 다양한 환경이 미리 준비 되어 있다. ( 없다면 마켓플레이스 - 이클립스 HELP 메뉴 - 를 통해 설치한다.)
2. File > New > New Maven project 선택
3. New Maven project 에서
org.apache.maven.archetypes 를 선택,
Artifact Id : 가 WebContext 명이 되며, Group Id 와 Artifact Id 를 합쳐 패키지 명이 생성 되도록 설정 하였다.
이후, 마치게 되면, Dynamic Web Project 와 같이 톰캣을 선택하여 실행 할 수 있다.
4. pom.xml 을 아래와 같이 편집하여 Spring 과 Web 개발이 가능 하도록 라이브러리를 추가 한다.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.sample</groupId>
<artifactId>SampleSpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleSpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>provided</scope>
</dependency>
<!-- Core utilities used by other modules.
Define this if you use Spring Utility APIs
(org.springframework.core.*/org.springframework.util.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Expression Language (depends on spring-core)
Define this if you use Spring Expression APIs
(org.springframework.expression.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Bean Factory and JavaBeans utilities (depends on spring-core)
Define this if you use Spring Bean APIs
(org.springframework.beans.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Aspect Oriented Programming (AOP) Framework
(depends on spring-core, spring-beans)
Define this if you use Spring AOP APIs
(org.springframework.aop.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Application Context
(depends on spring-core, spring-expression, spring-aop, spring-beans)
This is the central artifact for Spring's Dependency Injection Container
and is generally always defined-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Various Application Context utilities, including EhCache, JavaMail, Quartz,
and Freemarker integration
Define this if you need any of these integrations-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Transaction Management Abstraction
(depends on spring-core, spring-beans, spring-aop, spring-context)
Define this if you use Spring Transactions or DAO Exception Hierarchy
(org.springframework.transaction.*/org.springframework.dao.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- JDBC Data Access Library
(depends on spring-core, spring-beans, spring-context, spring-tx)
Define this if you use Spring's JdbcTemplate API
(org.springframework.jdbc.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA and iBatis.
(depends on spring-core, spring-beans, spring-context, spring-tx)
Define this if you need ORM (org.springframework.orm.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX,
Castor, XStream, and XML Beans.
(depends on spring-core, spring-beans, spring-context)
Define this if you need OXM (org.springframework.oxm.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Web application development utilities applicable to both Servlet and
Portlet Environments
(depends on spring-core, spring-beans, spring-context)
Define this if you use Spring MVC, or wish to use Struts, JSF, or another
web framework with Spring (org.springframework.web.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Spring MVC for Servlet Environments
(depends on spring-core, spring-beans, spring-context, spring-web)
Define this if you use Spring MVC with a Servlet Container such as
Apache Tomcat (org.springframework.web.servlet.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Spring MVC for Portlet Environments
(depends on spring-core, spring-beans, spring-context, spring-web)
Define this if you use Spring MVC with a Portlet Container
(org.springframework.web.portlet.*)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- Support for testing Spring applications with tools such as JUnit and TestNG
This artifact is generally always defined with a 'test' scope for the
integration testing framework and unit testing stubs-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SampleSpringMVC</finalName>
</build>
</project>
5. 아래와 같이 프로젝트를 업데이트 한다.
6. 프로젝트를 선택후 New Source Folder를 통해 소스 디렉터리를 생성. 여기에 Controller 등을 생성한다.
7. web.xml 편집시 확인 사항.
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
스프링 MVC의 사용을 위해 위와 같이 내용을 변경 하게 되어지는데,
MAVEN 버전에 따라 다르겠지만,
1. web.xml을 수정.
2. pom.xml에 dependency 를 추가하여 작업을 하면,
당 Project 모듈에 대하여 업데이트를 할수 없다는 오류를 뱉어 낼것이다.
<!-- Maven Webapp 의 web.xml 의 최초 생성 선언 -->
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
이럴 때, 번거롭지만, 위의 2_3.dtd 소스로 수정후 프로젝트 업데이트를 한후,
다시 원복 하면, 정상적으로 웹 프로젝트가 작동한다.
8. web.xml 편집.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>SampleSpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SampleSpringMVC</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
9. DispatcherServlet 생성
web.xml 와 같은 위치에 생성 하며, web.xml 에서 설정한 <servlet-name> + "-servlet.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- HandlerMapping -->
<!-- 정의 없음
요청 URL과 매핑되어지는 Controller 검색에 쓰이며,
다음과 같은 형태로 정의 되어 진다.
<bean id="beanNameUrlMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean id="sampleController" name="/my.html" class="main.test.sampleController">
-->
<!-- Controller -->
<bean id="indexController" name="/index.html" class="controller.IndexController"
p:shopService-ref="shopService">
</bean>
<!-- ViewResolver -->
<!-- 정의 없음
Controller 를 통해 발생된 모델과 뷰정보의 실행 결과를 보여줄 View 검색
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
<!-- Data Source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- JDBC 드라이버 클래스명 설정-->
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<!-- JDBC 접속 문자열 설정 -->
<property name="url">
<value>jdbc:mysql://localhost:3306/test?characterEncoding=euckr</value>
</property>
<!-- MySQL 유저ID 설정 -->
<property name="username">
<value>root</value>
</property>
<!-- MySQL 패스워드 설정 -->
<property name="password">
<value>cyon0315</value>
</property>
</bean>
<!-- 각종 Bean 생성 -->
<bean id="shopService" class="logic.ShopImpl" p:itemCatalog-ref="itemCatalog">
</bean>
<!-- ItemCatalog -->
<bean id="itemCatalog" class="logic.ItemCatalogImpl"
p:itemDao-ref="itemDao">
</bean>
<!-- ItemDao -->
<bean id="itemDao" class="dao.ItemDaoImpl" p:dataSource-ref="dataSource">
</bean>
</beans>
=====================================================================================================
이상으로, Maven 을 이용하여 Spring + Maven 설정을 마친다.
각 컨트롤러의 처리나, Domain 모델등에 대해서는 필요에 따라 인터넷으로 많은 정보가 있다.