Monday, September 14, 2015

How to setup Spring MVC project with maven in Eclipse

There article shows how to create a spring MVC project with maven in Eclipse. The eclipse comes with an internal maven-archetype-webapp for webapps, but is somehow a little bit out of date. The project created by that needs some extra work to make it workable.

In this demo a maven managed Spring MVC helloworld  webapp is created in Eclipse. This demo doesn’t try to show how to use Spring MVC, but focus on how to set up the development environment in Eclipse.

0. What you need

  • JDK 1.7 +
  • Eclipse IDE for Jave EE (version 4.4 Luna)
  • Maven 3.2.1

1. Create a maven project in Eclipse

Create a new maven project.

image

Make sure choose the archetype maven-archetype-webapp

image

Then next until finish. You may notice that the new created project has errors. In Eclipse IDE, it has red crosses like below.

image

Don’t worry about that, we will deal with the errors step by step until it becomes a runnable one.

2. Configure the project

There are several things need to be changed for the eclipse created project.

2.1 Configure maven pom.xml

Since this is a maven managed project, the JDK version need to be set in pom.xml, not in Eclipse. The dependency Spring MVC is also added to pom.xml

<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.shengwang.demo</groupId>
<artifactId>spring-mvc-helloworld</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>spring-mvc-helloworld Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
<!-- Use Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>

<build>
<finalName>spring-mvc-helloworld</finalName>
<!-- Use Java 1.7 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

After pom.xml saved and use Alt-F5 to update the maven project, Eclipse still complain about an error for the project like below.


It’s a error about “javax.servlet.http.HttpServlet” was not found on the Java Build Path.


image


Don’t worry, The class should be provided by the Servlet containers, such as Tomcat, we need to do more configuration for the running environment of the project. So let deal with it now.


2.2 Configure Dynamic Web Module for project


It’s a little bit tricky to change Dynamic Web Module version in Eclipse, first you need to unset the default old version 2.3 in project facets, then set it again with new version 3.0 or higher. Direct change the version number will cause an error in Eclipse like this.


image


So let do it in the correct way, unset the old one, then set it again with new version.


Right click on the project name – > choose properties. Uncheck the Dynamic Web Module 2.3, then hit OK to confirm.


image


Again, right click on the project name – > choose properties. Check the Dynamic Web Module 3.0, Also set specify a runtime server for the our helloworld webapp.


image


Now our helloworld Spring MVC web app don’t have any errors in Eclipse.


image


2.3 Add java directory


The wizard created project don’t have directory for java code, we need to create this directory by hand. Like other maven project,  /src/main/java is the folder need to be created to put the java code.

Now if you continue to proceed this tutorial, configure web.xml and dispatcher-servlet.xml, you will end up with a spring mvc project with configuration in XML file. If you prefer to have a spring mvc project with java-based configuration, you can skip the rest of this article and jump to another tuorial on Java-based Spring mvc configuration - without Spring security


2.4  basic web.xml configuration


Change the default /src/main/webapp/WEB-INF/web.xml to the basic configuration below .

<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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<display-name>Demo for mvc hello world config</display-name>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- specify location of spring config, don't use default --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

The version="3.0" means using servlet version 3.0. the Spring configuration file is name "dispatcher-servlet.xml" in classpath, usually means /src/main/resources/dispatcher-servlet.xml. Just remember don't use the default value for contextconfigLocation, which will be /src/main/webapp/WEB-INF by default, to avoid hitting rocks when doing integration test later.


2.5 basic spring configuration


Add a new file /src/main/resources/dispatcher-servlet.xml. In the same directory as web.xml, the basic spring configuration looks like below.

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

<context:component-scan base-package="com.shengwang.demo" />

<mvc:annotation-driven/>

<!-- ================================== -->
<!-- 1. mapping static resources -->
<!-- ================================== -->
<mvc:resources location="/static-resources/css/" mapping="/css/**" cache-period="3600"/>
<mvc:resources location="/static-resources/img/" mapping="/img/**" cache-period="3600"/>
<mvc:resources location="/static-resources/js/" mapping="/js/**" cache-period="3600"/>


<!-- ================================== -->
<!-- 2. view resolver for JSP -->
<!-- ================================== -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

Change the base package to your own. In this demo it’s com.shengwang.demo. The static resources directory can be any directories relative to /src/main/webapp


Now the project looks like this.

image

Since this article focuses on how to setup project and development environment for spring mvc with maven in Eclipse. The spring mvc coding part, such controllers and business logic are omitted. Now your maven managed Spring MVC project is setup in Eclipse, ready for some java code and run.


Happy coding!

0 comments:

Post a Comment

Powered by Blogger.

About The Author

My Photo

Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.

Pages

Unordered List