Life of the IT-Drummer

A web log from Tobias Rusås Olsen.

Basic Spring 3.0 eclipse-project with Maven 3

There are already lots of tutorials for Spring and for Maven, but many seemed outdated, and it was a challenge to collect all the pieces, so I’ve done it for you (and for the future me). Just follow these instructions! This is pretty much a “Getting started”, and it does not contain much details. If there are any questions or remarks, please use the comment field.

Download Maven:

http://maven.apache.org/download.html.

To be able to run mvn from the command line, add this to your environment variable “path”: ${install_folder}\apache-maven-3.0.2\bin. Replace ${install_folder} with your install folder, obviously.

Open a terminal and write mvn and verify that it runs.

Go to your eclipse workspace, create a new folder and add a pom.xml-file. This is Mavens “Project Object Model” which is used for configuring the project. I won’t go into details, but most lines define dependencies needed for Spring. Add this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<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.saiboten.testapp</groupId>
  <artifactId>testapp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>testapp</name>
  <url>http://maven.apache.org</url>
 
  <build>
 
  <plugins>
  <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
 
        <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
        </configuration>
</plugin>
</plugins>
</build>
 
<!-- Shared version number properties -->
<properties>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
 
<dependencies>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${org.springframework.version}</version>
</dependency>
 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${org.springframework.version}</version>
  <scope>test</scope>
</dependency>
 
<dependency>
    <groupId>geronimo-spec</groupId>
    <artifactId>geronimo-spec-j2ee</artifactId>
    <version>1.4-rc4</version>
</dependency>
 
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
 
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2</version>
</dependency>
 
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.1.2</version>
</dependency>
 
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.5</version>
</dependency>
 
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
 
</dependencies>
</project>

Create the following file structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
root
-src
--main
---java
---resources
---webapp
----META-INF
----WEB-INF
-----jsp
 
-test
--main
---java

Open a new terminal window, navigate to the folder with the pom.xml, and write mvn eclipse:eclipse. This will create a new eclipse project based on the file structure and the contents of the pom.xml.

Import the project into eclipse by using “Existing projects into Workspace” in the import meny. The root dir is the testapp-dir of your new project.

Add a web.xml-file in the src/main/webapp-folder with the following contents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>testapp</display-name>
 
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>
 
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class> org.springframework.web.servlet.DispatcherServlet 
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
 
</web-app>

Also add a dispatcher-servlet.xml in the same folder, it should have this content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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">
	<context:annotation-config />
 
	<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/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
 
 
</beans>

Let’s create some classes. First we need a controller. I have called mine DefaultController. Annotate the class with @Controller, and add a method called helloWorld, and annotate it with @RequestMapping(”hello”). Requests with path “hello” will be forwarded to this controller by the dispatcher servlet.

The full class looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.saiboten.testapp.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class DefaultController {
 
	@RequestMapping("hello")
	public ModelAndView helloWorld() {
		ModelAndView mav = new ModelAndView("hello");
 
		mav.addObject("hello", "Hello Spring!");
 
		return mav;
	}
 
}

For the class to be made available in the application context, we need to scan the package for components. Add this line to your dispatcher-servlet.xml:

1
<context:component-scan base-package="com.saiboten.testapp.controller" />

Now we need a jsp-file called “hello.jsp”. Add it in the jsp-folder underneath WEB-INF. Add the following contents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Hello Spring!</title>
</head>
<body>
<p>${hello}</p>
 
</body>
</html>

Add a log4j.properties-file in the src/main/resources folder. This will attach a log-appender to the project.

1
2
3
4
5
6
7
8
9
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
 
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
 
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Go to the terminal window again and write “mvn install jetty:run”. This will compile the project, install it to your local maven repository and start the lightweight application server jetty by using the maven-jetty-plugin defined in your pom.xml. Open your browser and navigate to: http://localhost:8080/testapp/hello.

4. ?
5. Profit

RSS 2.0 | Trackback | Comment

2 Responses to “Basic Spring 3.0 eclipse-project with Maven 3”

  1. Antonello Mangone

    In your tutorial you write:

    For the class to be made available in the application context, we need to scan the package for components. Add this line to your dispatcher-servlet.xml:

    but you don’t write the line … could you add it please ?


  2. Done! :-) Thanks for noticing.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">