This is part 3 of a series of posts describing a Hello World web application built using Eclipse, Maven, Sysdeo, and Spring Annotations.  In part 1, we used a Maven archetype to build the initial structure of our application, and configured it as a Sysdeo Tomcat project.  In part 2, we set up the web application’s web.xml file, including a line-by-line walkthrough.

In this third post, we will begin adding in the JAR files that our web application depends upon.  We’ll use Maven to add those dependencies.

Note: You’ll need to have Sysdeo and the Maven Integration for Eclipse installed to follow along.  See Java Projects for more information on installing these.

If you haven’t replaced your /src/main/webapp/WEB-INF/web.xml with the web.xml from Part 2 yet, go ahead and do that now.

Adding Dependencies with Maven

In part 2, we set up our web application’s web.xml file.  You may have noticed that we referenced several classes in our web.xml that aren’t actually in our classpath, such as "org.springframework.web.context.ContextLoaderListener".  As you may know, this class is actually part of the Spring Framework for applications.  We need to add the necessary Spring classes (and a few others) to make this work correctly.

First, let’s decide what dependencies we are going to need.  Here’s the list:

  • We’ll need the Spring Framework’s 3.0.0 web-MVC packages
  • We have also configured Log4J in our web.xml, so we will need log4j 1.2.14
  • Part of the requirements of the project are to use JSP pages to show our message. So we’ll need JSTL 1.2 to handle some of the common JSP tags and tasks.

Next, let’s look at what our project structure looks like at the moment:

Project-Folders-Start

If you are new to Maven, you may notice a new library set called "Maven Dependencies".  As you may have guessed, this is where Maven shows you which JAR files it is adding to your web application’s classpath.  Currently there is only junit 3.8.1 in the dependencies, which is the default setup for a Maven project.  You may also notice the pom.xml file.  This is the configuration file for Maven for your project.

Let’s have a look at the pom.xml file:

<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.technologicaloddity</groupId>
  <artifactId>HelloWebApp</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloWebApp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>HelloWebApp</finalName>
  </build>
</project>

So that is where junit came from.  Don’t worry about the exact format of this file, but if you are interested, you can learn more at the Maven Site.

Let’s add some of our dependencies.  Let’s take the first one from the list above: Spring MVC 3.0.0.

Inside Eclipse, right-click on the pom.xml file in the HelloWebApp project.  Select Maven > Add Dependency as shown below.

Maven-AddDep-Menu

This will bring up the Maven Add Dependency dialog.  In the filter box type "org.springframework".  Find "org.springframework spring-webmvc".  Inside this section, find "3.0.0.RELEASE – spring-webmvc-3.0.0.RELEASE.jar".  (Make sure you get this one, and not the similarly named sources-jar).  Leave the Scope option on "Compile".  Click OK.

Maven-AddDep-SpringMVC

So what did this do?  Open up your pom.xml file to find out.  You should see this in your dependencies sections now:

  <dependencies>
    <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>3.0.0.RELEASE</version>
    </dependency>
  </dependencies>

Maven has added the request file to our pom.xml file (spring-webmvc).  Big deal.  You could have done that just as easily by hand right?  But it did more than that.  Inside your HelloWebApp project structure, you should see an item called "Maven Dependencies".  This is where Maven puts JAR files that will eventually be in your webapp’s classpath.  (Note that the location of "Maven Dependencies" varies based on your view.  Usually it is either right under the project root, or inside "Java Libraries".)

Maven-Deps-AfterMVC

Whoa!  We knew it had junit and spring-webmvc, but where did all that other stuff (like spring-beans, aopalliance and spring-core) come from?  Maven automatically detects the dependencies of the JAR files that you add to the project and adds them to your project IN ADDITION TO the file that you requested.  Spring-WebMVC needs Spring-Core, Spring-Beans, AOP, and others to run correctly, so Maven automatically added them here. 

This is a very good thing.  If Maven didn’t grab dependencies of your dependencies, you would have to choose each dependency separately.  Ever built a classpath directory by hand, and had to download 100 JAR files to put in it?   Maven takes care of all that.

OK, Let’s do our other dependencies now.  Right click on the pom.xml file, and select Maven > Add Dependency.  Type log4j in the filter box.  Select the "log4j log4j" item, and add in 1.2.14 to your project.  (I don’t recommend adding 1.2.15 as it is configured differently in Spring than 1.2.14.  1.2.14 is fine for us right now).

Follow the same steps for our last dependency: JSTL 1.2.  Right-click on the pom.xml file, select Maven > Add Dependency.  Type "jstl" in the filter box.  Select the "javax.servlet jstl" item, and add in jstl-1.2.jar.  (Again, make sure you get the normal JAR, not the "sources" JAR).

After adding the three dependencies (spring-webmvc, log4j, and jstl), your pom.xml file should look 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.technologicaloddity</groupId>
  <artifactId>HelloWebApp</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloWebApp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <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>3.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>HelloWebApp</finalName>
  </build>
</project>

And your dependencies should look something like this:

Maven-Deps-AfterAll

Remember the DevLoader setup we did in Part 1 of this series?  All the JAR files in the Maven Dependencies folder will be automatically added to the DevLoader when you run Tomcat.  How does it do that?  Through the MAVEN2_CLASSPATH_CONTAINER variable that we checked at the bottom of the DevLoader screen.  Maven sets the list of JAR files that it needs in this variable, and in turn Sysdeo passes that variable onto Tomcat.  This means you don’t have to worry about your CLASSPATH at all.  Sysdeo and Maven handle it all for you.

But in the end, you need to build a WAR file, not just develop on Eclipse!  Don’t worry.. Maven is great at making WAR files, as we will see in Part 4.

In Part 4, we’ll finish the setup with some configuration files for Spring and Log4J, and start our Tomcat server with Sysdeo for the first time.

One Response to “Hello World Web App using Spring Annotations Part 3”

  1. Venkat Mantirraju says:

    I love this explanation and I learned a lot from this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *