First Spring Web MVC on Eclipse with Maven
September 7, 2010 2 Comments
Create a new ‘Dynamic Web Project’.

Name it, say ‘SpringMaven’.

Create 2 source folders. ‘src/main/java’ holds all java classes & ‘src/main/resources’ holds external resources.

Set the Content Directory.

Under ‘src/main/java’ source folder let create 2 packages. ‘com.dyutiman.spring.controller’ will hold all controller classes and ‘com.dyutiman.spring.formbean’ will have all our formbeans mapped to our view forms.
Let create a package for our resources, ‘com.dyutiman.spring.res’.
Create the ‘views’ folder under src -> main -> java -> webapp -> WEB-INF to hold all view files.
Now as the project is created, next step is to configure Spring. Open ‘web.xml’ from src -> main -> java -> webapp -> WEB-INF -> web.xml and
add these lines with other necessary configuration options
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SpringMaven</display-name> <servlet> <servlet-name>anyname</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>anyname</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.htm</welcome-file> </welcome-file-list> </web-app>
When we say our DispatcherServlet name is ‘anyname’, we also have to create another xml file under WEB-INF named ‘anyname-servlet.xml’ which will have all Spring Web MVC specific components (beans) which say where to find application controllers and how to resolve views.
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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">
<!-- location of the annotated controllers -->
<context:component-scan base-package="com.dyutiman.spring.controller" />
<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/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Now the project is set up but till we haven’t include the Spring libraries to our classpath. We will let Maven to do the job for us.
Create ‘pom.xml’ at project root. The file looks something like this
<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>com.dyuti.spring</groupId>
<artifactId>spring-1-</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>spring-1</name>
<build>
<finalName>SpringMaven</finalName>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<filtering>false</filtering>
<excludes>
<exclude>WEB-INF/lib/*</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${commons.beanutils.version}</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons.lang.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>1.6</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.1</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<artifactId>ehcache</artifactId>
<groupId>net.sf.ehcache</groupId>
</exclusion>
<exclusion>
<artifactId>commons-collections</artifactId>
<groupId>commons-collections</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>${hibernate.annotations.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>${hibernate.annotations.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>${jpa.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-orm</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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<log4j.version>1.2.16</log4j.version>
<slf4j.version>1.6.1</slf4j.version>
<hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
<hibernate.version>3.2.6.ga</hibernate.version>
<jpa.version>1.0</jpa.version>
<spring.version>3.0.0.RELEASE</spring.version>
<jstl.version>1.1.2</jstl.version>
<commons.beanutils.version>1.7.0</commons.beanutils.version>
<commons.collections.version>3.2</commons.collections.version>
<commons.io.version>1.3.2</commons.io.version>
<commons.lang.version>2.3</commons.lang.version>
</properties>
</project>
Thats all for Maven and to build the project with Maven run the following command from command line from the root of the project
mvn eclipse:clean eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true -Dwtpversion=2.0
When everything is set, lets create a simple form with validation on it.
for reference
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html
alternate pom.xml for Spring MVC withMaven in eclipse IDE
4.0.0
com.ava.app
Spring_Maven
1.0-SNAPSHOT
war
Spring_Maven
http://maven.apache.org
UTF-8
Spring_Maven
install
org.apache.maven.plugins
maven-war-plugin
2.1.1
connection
org.apache.maven.plugins
maven-compiler-plugin
1.6
1.6
junit
junit
3.8.1
test
org.springframework
spring-beans
3.0.1.RELEASE
org.springframework
spring-asm
3.0.1.RELEASE
org.springframework
spring-context
3.0.1.RELEASE
org.springframework
spring-context-support
3.0.1.RELEASE
org.springframework
spring-core
3.0.1.RELEASE
org.springframework
spring-expression
3.0.1.RELEASE
org.springframework
spring-web
3.0.3.RELEASE
org.springframework
spring-webmvc
3.0.3.RELEASE
javax.servlet
servlet-api
2.5
provided
org.antlr
antlr-runtime
3.1.3
commons-logging
commons-logging
1.0.4
its very helpful to me , bcoz i was breaked with samples.