I’ve used the Spring Framework to build web applications for several years now.  I learned the old-fashioned way (if there is such a thing) of Spring MVC: SimpleFormController, AbstractController, Wizard controllers, and the rest of the MVC package.  Recently though, I had a chance to start a project from ground zero and upgraded to Spring 3.0.0.  It was also a good chance to learn about Spring Annotations and how they could make my life easier.

This is the first of what I hope to be a series of posts about building web applications using the Spring Framework.  If you are completely new to Spring, this post will hopefully help you get over the initial hump of learning Spring.  If you are familiar with Spring, but not with annotations for web applications, these posts will give you some examples.

Like most of the Java projects on this site, I’ll be using Eclipse, Maven, and the Sysdeo Tomcat plug-in.  You may want to read the Using Java Projects and Setting up Sysdeo postings for more information.

In this first post, we will build a version the classic Hello World as a web application using Spring.  Here are the requirements and goals:

  • Use a Spring MVC controller and a JSP page to show a simple message
  • Use Spring Annotations instead of the old MVC package
  • Minimum amount of XML configuration (in fact, it is 27 lines of XML in the end)
  • The message must come from the controller, not be hard-coded into the JSP
  • Package the web application into a WAR file (using Maven)

Setting up the Project

Before beginning: Make sure you have Sysdeo and the Maven Integration for Eclipse installed!  See Using Java Projects for more information.

To begin, let’s set up a new Eclipse Maven web app project.  Start Eclipse, then select File > New > Project.  From that dialog box, select Maven > Maven Project (see below).


On the next screen, make sure that "Create a simple project" is NOT checked, and that "Use default workspace location" IS checked.  Click next.


We will be using the Maven WebApp Archetype to build this application.  After you select Maven Project and select the default location, you should see the list of archetypes.  Filter this list on the word "webapp", and select maven-archetype-webapp version 1.0 (see below).  Click next.

Note: if you don’t see any archetypes in the list, make sure you have installed the Maven plug-in and the Maven Central Repository option for the plug-in.


Now, let’s give our project a name and some structure on the next screen.  Fill in Group ID of "com.technologicaloddity", artifact ID of "HelloWebApp", version "0.0.1-SNAPSHOT", and package "com.technologicaloddity.hellowebapp".  Click finish.  The new project should appear in your workspace, named "HelloWebApp".


Now, lets do a bit of project maintenance to get everything just right.

First, create a directory inside your project structure called /src/main/java.  Right-click on this folder after creation and select Build Path > Use as Source Folder.  This will tell Tomcat where your JAVA files live.

Next, right click on the HelloWebApp project and select Properties.  Select "Java Compiler" from the list and make sure that this project is using Compiler Compliance Level 1.6.  (Note: In theory you could also use 1.5, but I haven’t tested it that way).

While still in the HelloWebApp project properties, select "Tomcat".  (Note: This option is provided by the Sysdeo Tomcat plug-in. If you aren’t using the Sysdeo Tomcat plug-in here, you need to set up your project with whatever platform you are using).

Check all the checkboxes on this screen (see below for a screenshot).  Set the Context Name to "/HelloWebApp".  This becomes your context URL in Tomcat (for example http://localhost:8080/HelloWebApp/something.jsp).   Leave "Extra Information" blank (we’ll use it in a later project though).  For "Subdirectory to set as web application root" use "/src/main/webapp".  This is where Tomcat will look for non-class files, such as JSP and HTML files.


Next, click on the "DevLoader Classpath" tab on the same screen.  Check "Activate DevLoader".  Below in the list of files, check "/HelloWebApp/target/classes" (this should be the first in the list) and also check "org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER" (which is probably the last one in the list).  Click "OK" to save your changes.

HelloWebApp/target/classes is where your compiled CLASS files live (which are compiled from your JAVA files in /src/main/java).  The MAVEN2_CLASSPATH_CONTAINER will keep track of the dependencies for your project and add in all the JAR files that you need to run the web application.


Since the Maven webapp archetype comes with a preloaded JSP, you should now be able to start your Tomcat server by clicking on the Start Tomcat button Start-Tomcat-Button (if your are using Sysdeo), and opening a browser to http://localhost:8080/HelloWebApp/index.jsp .

So we have our web application running on Tomcat inside Eclipse with Maven support.  We still have some work to do, and in Part 2 we’ll modify our web application’s web.xml file and walk through it line by line.

7 Responses to “Hello World Web App using Spring Annotations Part 1”

  1. realy nice 🙂 sometimes i wish every blogger would pay such attention to their post as you do, keep posting

  2. Hey I am so grateful I found your web site, I really found you by mistake, while I was looking on Bing for something else, Anyways I am here now and would just like to say thanks for a incredible post and a all round exciting blog (I also love the theme/design), I don’t have time to read it all at the moment but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the excellent job.

  3. hey it was really helpful for beginners thanks a lot dude

  4. Hey… your blog is so help full… keep posting… 🙂

  5. amazing. one of the best blogs I’ve ever come across.

  6. Howdy just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Internet explorer.
    I’m not sure if this is a formatting issue or something to do with web browser compatibility but I figured I’d post to
    let you know. The layout look great though! Hope you get
    the problem solved soon. Cheers

  7. Great blog here! Also your website loads up fast! What host are you using?
    Can I get your affiliate link to your host? I wish my web site loaded
    up as fast as yours lol

Leave a Reply

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