1. Environment Summary
OS: Windows 7 Ultimate 32 bit
Eclipse: Juno
Tomcat: 7.0
Maven: 3.0
Spring: 3.2.2
2. Create a new Maven Project
Eclipse File-> New -> New Project -> Maven Project, tick on the 'Create a simple project' checkbox.
Enter necessary information on the subsequent dialog boxes, and finish.
Now, we got this structure:
3. Add project facet(optional)
In the Eclipse project explorer, right click the project name, and select the 'Properties', go to the 'Project Facets', click the 'Convert to facet form',
4. Add WEB-INF folder and configure web.xml
We can copy this folder from other projects.
Configure the DispatcherServlet in the 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" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>sphere</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>sphere</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
5. Complete the pom.xml and ensure the Maven dependencies
<properties> <spring.version>3.2.2.RELEASE</spring.version> </properties> <dependencies> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- Logback and slf4j --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.11</version> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> </dependencies>
6. Create Application Context Configuration.
As we configured a servlet named "sphere" in web.xml, thus we can create an Application Context Configuration file named "sphere-servlet" under WEBINF directory. Note that this is good enough for demo, but not good enough for production.
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <mvc:resources mapping="resources/**" location="/resources/" /> <mvc:annotation-driven /> <context:component-scan base-package="com.derek" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
Integrate Tomcat is extremely easy with contemporary Eclipse, just needs a couple of stupid steps as follow:
7.1 Add Server Runtime Environments
Eclipse -> Window -> Preferences -> Server --> Runtime Environments --> Add --> Apache --> Apache Tomcat v7.0 -> Next -> Input Tomcat Installation Directory -> Finish.
7.2 Add new server
In Servers view, right click -> new -> Server
7.3 Add application to the server
Right click the new created server in Server view --> Add and remove