Spring Set Up

springmvc

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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>testspringmvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>testspringmvc Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
          <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <type>maven-plugin</type>
</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>testspringmvc</finalName>
  </build>
</project>

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:

<!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>springDispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/todo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springDispatcher</servlet-name>
    <url-pattern>/spring-mvc/*</url-pattern>
</servlet-mapping>
</web-app>

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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-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 />
</beans>

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