Spring Set Up


The overall steps (at least 6) to create a SpringMVC project (beside using the Maven webapp archetype):

1. Install Eclipse
2. Install Maven
3. Use the m2eclipse plugin to create a project using the webapp archetype
    1. Launch Eclipse
    2. Click on File -> New -> Project
    3. Type "maven" into the "type to filter" text box
    4. Click on "Maven Project"
    5. Click Next
    6. Select or unselect appropriate check box
    7. Click Next
    8. Select the "webapp" archetype
    9. Click Next
4. Add Spring MVC as a dependency (see below)
5. Add the Dispatcher Servlet to web.xml file (see below)
6. Create the Spring Application Context by adding the “application context” xml file (see below)

To add Spring MVC as a dependency:

1. Double click on the pom.xml in Eclipse
2. Click on the pom.xml tab
3. Edit the pom.xml file to look like:
<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">
  <name>testspringmvc Maven Webapp</name>

To add the Dispatcher Servlet to web.xml file: When we create the maven project using the webapp archetype, it creates the web.xml under src/main/webapp/WEB-INF. Open this file and modify it like:

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

  <display-name>Archetype Created Web Application</display-name>


In the above code, we gave the dispatcher servlet a name "springDispatcher", and we map the URL pattern /spring-mvc/* to be handled by this springDispatcher servlet. This servlet also require additional configuration via a "application context" configuration file.

To create the Spring "Application Context" configuration file: From the step above, we gave the "application context" configuration file a name "todo-servlet.xml". To add this file:

1. Go to src/main/webapp in Eclipse
2. Right click on the WEB-INF folder
3. Select New -> File -> Other
4. Type xml into the "type to filter" text box
5. Click Next
6. Give it a name (todo-servlet.xml)
7. Click Finish
8. Modify the todo-servlet.xml to look like:
<beans xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.companyName" />
    <mvc:annotation-driven />

To add a new server to Eclipse: Right click anywhere in the Servers view and select "New -> Server", or we can do it from "File -> New -> Other" and type "server" into the "type to filter" text box.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License