Getting started with Spring-web-mvc and the Spring IDE-plugin – creating a simple web application
At the last KC (about Acegi Security) there was a discussion
about how easy it is making typo’s in Spring xml files. If you mistype
something your application won’t work and it can be difficult to track down
your error. Also typing all the class names is something you don’t want. A
former colleague told me about a plugin that does auto-completion and error
highlighting for your Spring configuration files (http://springide.org/)
Starting with Spring can be very boring, first you have to
learn all about acronyms like IOC, DI and AOP. Then there is a lot of talk
about POJO’s. What happened to the good old Hello World examples and then
explaining the details? Starting with a new framework is much nicer when you
have something working very quickly, the details are for later.
This article is a quick introduction to the web-part of the
Spring framework. You should have a Hello World application up and running in 15
minutes. And I’ll give a short introduction to the Spring-IDE plugin that makes
your live a lot easier.
Create your project and install the plugin
For this article I assume you have installed Eclipse 3.1
(with WTP), Maven2 (and the plugin for Eclipse) and some basic knowledge of
Struts really helps, but isn’t necessary.
Open a command prompt and run the following command in your
eclipse workspace directory to create a maven2 project:
mvn archetype:create -DgroupId=nl.amis -DartifactId=spring-ide-blog>
The next step is eclipsifying your project:
mvn eclipse:eclipse
Now start eclipse. Go the menu File -> import ->
existing projects into workspace and select the spring-ide-blog directory and
click finish.
To install the Spring-IDE plugin follow the next steps:
- Open
Eclipse. Go to Help -> Software Updates -> Find and Install. - Select
"Search for new features to install". Click "Next". - Click
"New Remote Site". Enter "Spring IDE updatesite" for
the Name and "http://springide.org/updatesite/" for the
URL. Click "OK". - Be
sure the checkbox for the “Spring IDE updatesite” is checked and click
finish - Expand
all the fields and check Spring IDE 1.2.5 - Finish
your installation and your Eclipse needs to be restarted
To make the plugin aware of a Spring project you should add
a Spring project nature to your project, this can be done by right clicking on
your project and select the Add Spring project Nature option. The tiny folder
will now display an even smaller S. Also enbale maven2 on your project and add
the following dependencies to your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
Add <packaging>war</packaging> to the pom.xml
so we easily can deploy a war-file on the application server
Create your web.xml file in src/main/webapp/WEB-INF and
insert the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
You now have to put the DispatcherServlet in your
application. This servlet is comparable with the Struts Action Servlet. Because
I have named the servlet dispatch my Spring configuration file must be named
dispatch-servlet.xml. Create this file in your WEB-INF directory and add the
following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>
Open the Spring Beans view (Window -> Show View ->
Spring IDE, Spring Beans)
Right click the spring-ide-blog application and click on
properties. Add the dispatch-servlet.xml with the add button (and select the
file).
Using the Spring IDE plugin
Now it is time to use the Spring IDE plugin. Do not
copy/paste the following code because then you won’t see the use of the plugin.
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"
/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
When you type very slowly you see the plugin auto-completing
your class names. Try changing the class name in something that makes no sense
and save your file. An error sign will appear before your line. (when nothing happens you have to
close the window for dispatch-servlet.xml en re-open it, there are still some
things that don’t work perfect with the plugin)

Spring uses something that works like an ActionForward in
Struts. In Spring it’s called a ModelAndView object and is a lot more than just
a forward. But treating it like an
ActionForward is enough for now. In Stuts you had to define all the
ActionForwards separately. In our Spring application this isn’t necessary, with
the InternalResourceViewResolver it’s enough to have a file that has (a part)
of the viewName of the ModelAndView object. When your ModelAndView object has
the viewName ‘success’ Spring will look for /WEB-INF/jsp/success.jsp.
The analogy with Struts doesn’t stop here. An important part
of Struts are Actions. In Spring the Actions are called Controllers.
Controllers return ModelAndView objects.
Creating the controller
It’s finally time to create the only java file you’ll need
today. Create a HelloWorldController.java in the nl.amis package.
package nl.amis;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloWorldController extends AbstractController {
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("success");
mav.addObject("string", "World");
return mav;
}
}
This controllers extends AbstractController, it’s the
simplest Controller there is in Spring. When you really start using Spring you
will need Controllers like the MultiActionController (DispatchAction in Struts)
and the SimpleFormController. We add an object called string to the
ModelAndView and then return the ModelAndView. The string object is used later
in the jsp.
We have to add a bean to the dispatch-servlet.xml to make
Spring aware of the HelloWorldController:
<bean name="/helloWorld.form" class="nl.amis.HelloWorldController"/>
Create success.jsp in the /src/main/webapp/WEB-INF/jsp
directory:
<html><head></head><body>
Hello ${string}
</body></html>
Deploy and preview your application
In the command prompt do a mvn pakacage and deploy the .war file in the target directory of your
favourite container (in my case Tomcat running on port 8080)
Visit this easy to remember url to check the results: http://localhost:8080/spring-ide-blog-1.0-SNAPSHOT/helloWorld.form
The page displayed should contain Hello World. We now have a
basic Spring application without scary terms like AOP en IOC.
It’s not nice to hardcode the string World in the
application. We want to define that in the configuration file. In Spring you
can add properties to you bean and use them in your application. We want to add
a property called output with the value Jeroen (for the people not called
Jeroen, use your own name). Our bean now looks like this:
<bean name="/helloWorld.form" class="nl.amis.HelloWorldController">
<property name="output" value="Jeroen" />
</bean>
Save your file. That’s nice, the Spring IDE tells us that we
forgot something! When we hover the red sign it says ‘No setter property found
for output’ with the class name. Create a String with name output and generate
the setter for it. After that replace “World” with output:
public class HelloWorldController extends AbstractController {
private String output;
public void setOutput(String output) {
this.output = output;
}
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("success");
mav.addObject("string", output);
return mav;
}
}
Do a mvn package and deploy your application again and the
application says Hello <insert your name here>. What you just did was a
case of die hard setter Dependency Injection (DI) or Inversion of Control.
That’s what all the fuzz is about. Now run to your colleagues and tell them you
know DI and IoC. The purpose of DI is that your application isn’t aware of the
fact that you hard-coded a String in your dispatch-servlet.xml. It could even
have been coming from a jdbc connection or a file on disk.
Conslusion
Another nice thing about the Spring IDE plugin that doesn’t
really fit in this article is the Plugin graph view. It can be found by right
clicking on a bean in the Spring Beans view. Right now it’s completely useless
but when you’re using many beans with dependencies this can be quite useful.




13/4/2006 - 6:55 pm
Thanks a lot for the instructions Jeroen. Ever heard of this plugin:
http://www.jamesholmes.com/spring/index.html
It support a wider range of IDEs but I never tried to use it myself.
Greets, Wouter
14/4/2006 - 2:55 am
Good stuff, thanX.
14/4/2006 - 12:57 pm
OK so I checked out the plugin at the URL I provided. It looks like the project is dead, so forget about it.
Wouter
29/4/2006 - 10:58 pm
Just stumbled upon this tutorial, and wanted to say thanks a bunch
I’ve been trying to get started with spring web, looked for a quick kick-start before digging deep into the documentation, and this site is just what I’ve been looking for. Also looks great…
thanks !
14/11/2006 - 1:22 pm
Very Good Article
5/11/2007 - 1:36 am
HI,
I am not able to follow this through. When i Go the menu File -> import -> existing projects into workspace and select the spring-ide-blog directory :
It does not show any existing projects, so i am unable to click the finish button. It appears that i have no projects in spring-ide-blogs to select.
Any help will be great, i have been searching for a site like this for a while which can guide me thru setting up a Spring project.
I have eclipse 3.2 ans WTP, Jdk 1.5 and also Maven 2.0.7.\
Thanks in advance. Looking forward to hearing from you.
7/11/2007 - 9:01 am
Did you generate the eclipse project with mvn eclipse:eclipse? There should appear some files that start with a dot in the directory where you’re project is created (.classpath, .project and maybe more)