Spring Framework Tutorial – Hello World

In this tutorial, I will create a simple Spring framework application. It is designed to be simple and brief.

1. Set up development environment

IDE: Eclipse Indigo Classic
From update site: http://download.eclipse.org/releases/indigo/
Download all component under Eclipse Java EE Development Tools(for simplicity reason)

2. Create a “Dynamic Web Project” by using wizard

Create an .jsp file named with “index.jsp”. The code is automatically generated like the following:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
this is hello world. 
</body>
</html>

Run the project on server, and you should have a welcome page as follows:

3. Add Spring Framework support

Download: Spring Framework 3.1.0.RELEASE is the current production release (requires Java 1.5+)
From: http://www.springsource.org/download
Add the following jar files to “WebContent/WEB-INF/lib” directory.

commons-logging-1.1.1.jar
commons-logging-api-1.1.1.jar
spring-asm-3.2.0.M1.jar
spring-beans-x.jar
spring-content-x.jar
spring-core-x.jar
spring-expression-x.jar
spring-instrument-x.jar
spring-web-x.jar
spring-webmvc-x.jar

Change web.xml file to be the following:

<?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>HelloSpring</display-name>
 
   <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Create springapp-servlet.xml file under WEB-INF direction and copy the following to it:

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  <!-- the application context definition for the springapp DispatcherServlet -->
 
  <bean name="/hello.htm" class="springapp.web.HelloController"/>
 
</beans>

Create a class inside “Java Resources” as follows:

package springapp.web;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
 
public class HelloController implements Controller {
 
	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		return new ModelAndView("hello.jsp");
	}
 
}

File Hierarchy of a Spring framework project

Possible problem:

There can be various kinds of problems when start working with Spring framework. Here is the problem I met, and you can leave your comment if you have any other problem.

If you missing package errors in the “HelloController.java” file, possibly fix the problem by doing this:
Go to “Java Build Path” -> Libraries -> Add Library… -> Server Runtime ->
Select your Tomcat version from the list

Also don’t forget to create a jsp file called “hello.jsp”.

This is the the start, now you can following tutorials from Spring official website: http://static.springsource.org/docs/Spring-MVC-step-by-step/part2.html.

Related Articles: