• +62 (081) 322 209080
  • Choose your language:
  • id

Blog

Insights and ideas for software startups. We provide advice and insights for entrepreneurs.

Hello World Example using Spring Framework MVC 3.1

Hello World Example Using Spring Framework MVC 3.1

A “Hello world program” is a famous computer program that displays “Hello world” on the screen device. This program is typically one of the simplest programs possible in most programming languages. By tradition the “Hello world program” is to demonstrate the most basic syntax of a programming language.

In this tutorial, we show you how to create a Hello world example using Spring Framework MVC 3.1.

Requirements

The required technologies are following:

  1. Java Runtime Environment (JRE). Click here to download.
  2. Spring Tool Suite (STS). Click here to download.
  3. Spring Framework 3.1.0.M1. Click here to download.

We consider that you are familiar with installing Java Runtime Environment and Spring Tool Suite, so we can skip this parts.

Now, let’s start creating the Hello world example and open the Spring Tool Suite.

Open the Spring Tool Suite (STS)

Select the directory of your workspace:

Choose a Workspace Directory

Create a Java Dynamic Web Project

Right-click in the package explorer and select New -> Dynamic Web Project:

Create a Dynamic Web Project

Enter a project name and press finish button:

Enter a Project Name

Import the required JAR libraries

Into our web dynamic project, we import the Spring Framework MVC 3.1 libraries that are distributed from the Spring Framework 3.1.0.M1. The needed JAR libraries are following:

  1. commons-logging-1.1.1.jar
  2. org.springframework.asm-3.1.0.M1.jar
  3. org.springframework.beans-3.1.0.M1.jar
  4. org.springframework.context-3.1.0.M1.jar
  5. org.springframework.core-3.1.0.M1.jar
  6. org.springframework.expression-3.1.0.M1.jar
  7. org.springframework.web-3.1.0.M1.jar
  8. org.springframework.web.servlet-3.1.0.M1.jar

To import JAR libraries, copy the jar libraries mentioned above in the /WEB-INF/lib directory.

  1. Note: The /WEB-INF directory contains a Web archive’s private files and directories. It is automatically created after you create a dynamic web project. Click here to see the screen shot.
  2. Note: All jar libraries residing in the directory lib will be automatically loaded if the server is started.

Create a Deployment Descriptor – web.xml

The deployment descriptor – web.xml is a part of the servlet standard for Java WEB applications. It manages URL mapping, URL handling, URL authentication and other information.
The web.xml file must be written as it is and put in the /WEB-INF directory because of the Convention over Configuration of the Java Web Logic server.

Create an XML file with the filename web.xml in the /WEB-INF directory and add the following codes:

<?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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <display-name>hello-world-example</display-name>
  <servlet>
    <servlet-name>hello-world-example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello-world-example</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Explanation

  1. The tag <display-name> is optional. It specifies a short name that can be displayed by GUI tools.
  2. The tag <servlet>:
    1. The tag <servlet-name> is obligatory. It specifies the canocial name of the servlet.
    2. The tag <servlet-class> is obligatory. It specifies the fully-qualified class name of the servlet.
      the DispatcherServlet manages incoming URIs and finds the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combines to form the page or resource that is supposed to be found at that location.
  3. >The tag <servlet-mapping>:
    1. The tag <servlet-name> is the name of the servlet to which you are mapping a URL pattern. This name should be the same with the name you assigned a servlet in a <servlet> declaration tag.
    2. The tag <servlet-mapping> describes a URL pattern.

For more details, you can see the descriptor elements in the Java WebLogic server.

Register the Servlet

Create a main servlet with the name hello-world-example-servlet.xml in the /WEB-INF directory.
The hello-world-example-servlet.xml file must be written the same as the corresponding servlet name defined in the web.xml concatenated with “-servlet.xml” and put in the /WEB-INF directory and add the following codes:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
		
	<!-- Activates various annotations to be detected in bean classes -->
	<context:annotation-config />

	<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
	 For example @Controller and @Service. Make sure to set the correct base-package -->
	<context:component-scan base-package="com.flacom" />

	<!-- Configures the annotation-driven Spring MVC Controller programming model.
	Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
	<mvc:annotation-driven /> 
	
	<!-- Declare a view resolver -->
	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    		p:prefix="/WEB-INF/view/" p:suffix=".jsp" p:order="1"/>
    		
</beans>

Explanation

  1. The tag <context:annotation-config> is to manage various annotations to be detected in bean classes
  2. The tag <context:component-scan> is to scans the classpath for annotated components that will be auto-registered as Spring beans.
  3. The element <base-package=”com.flacom”> is the base package in which the annotated components reside
  4. The tag <mvc:annotation-driven> configures the annotation-driven Spring Framework MVC 3.1 Controller programming model.
  5. The bean jspViewResolver with the resolver class InternalResourceViewResolver is to manage all specified views (Java web pages such as JSP, Velocity, etc). In this project, we use JSP for our web pages.

For more details, you can see the Spring Framework MVC 3.1.x Documentation.

Create a Controller

After having set the Spring Framework MVC 3.1. configuration files, we create the controller to manage the view including URI. Right-click the src folder in the project, New and select create a package. We create a package with the name “com.flacom.web.controller” and press the button finish. After that, we create a java file inside that package with the file name “HomeController.java” by right-clicking the package, New and selecting Class.

Inside the “HomeController.java“, we put the following code:

package com.flacom.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value={""})
public class HomeController {

	@RequestMapping("/home")
	public ModelAndView home(){
		
		return new ModelAndView("home");
	}
}

Explanation

  1. The annotation @Controller is to register the bean “homeController” to the server. By annotating it, the class HomeController will be scanned by the annotation context.
  2. The annotation @RequestMapping is to register the URL of the class and also of the method. The @RequestMapping above the class is to define the main URL of the class followed by the @RequestMapping above the method home(). In this example, the valid URI for the method home() is “http://localhost:8080/hello-world-example/home“.
  3. The object ModelAndView defines which Java web page is referred to. We refer to a “home.jsp” which will be created after this.

Create a View

We create the last needed file namely the Java web page “home.jsp“.
First we create a folder “view” in the /WEB-INF directory by right-clicking the /WEB-INF directory, the select New, Folder and enter the folder name “view” and pressing the button Finish. After that, we create the file home.jsp inside the folder “view” we created just now and add the following code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Hello World Example</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>

Explanation

  1. The tags <html>, <head>, and <body> are the minimum requirement to define a JSP
  2. The tag <title> is to declare the title of the page
  3. The tag <h1> in the body is to display our text “Hello world” in the web browser.

The Project Structure

At the end, the structure of the project should look like following:

Structure of the Project in the Project Explorer

Prepare the server

Now, let’s run the web application and call it in the web browser.

Before the hello world example can be hit in the web browser, we have to register the hello world example in the Java server and and start it.

Add the Application in the Server

Register the hello world example in the Java server by right-clicking VMware vFabric tc Server Developer in the tab Servers and selecting Add and remove as displayed in the following screen shot:

Add the Web Project to the Server

and select the project hello-world-example, click add and press the button Finish:

Select the Project

Start the Server

After the project hello-world-example is added, we can start the server by clicking the start button as described below:

Start the Server

Call the URL and Get the Famous Hello World

If there is no error in the STS console, we can see our hello world example in the web broswer.
Open the web browser and type “http://localhost:8080/hello-worl-example/home” in the address field and press enter. You will see the following image:

Type the URL in Your Internet Browser to See the Result

Congratulation!

You created just now the famous Hello world example using Spring Framework MVC 3.1. Isn’t simple to create a web application using Spring Framework MVC 3.1? Tell us your opinion.

The full project source code (incl. jar libraries), you can download here – hello-world-example.jar.

If you may meet challenges, you can contact us. Please give feedback or comments in this post.

Jemmy Halim…

Jemmy is a person into technical details of developing WEB applications and their highly advanced back end. He earns his daily bread as a computer scientist and works on building WEB and Mobile Applications, Content Management Systems, and Money-Generating Software Solutions.
  • http://socialvideoceo.com Senaida

    Precisely how long did it acquire you to post
    “Spring Framework MVC 3.1 – Hello World Example”?
    It has got a bunch of fine knowledge. With thanks -Senaida

    • http://www.hlsystems.co.id Jemmy Halim

      Hi Senaida,
      Thanks for the comment. It took me some hours, though.
      I hope that it would encourage many people to start to build WEB applications using this cool Framework.
      Best regards,
      Jemmy

  • http://cnn.com Trey

    Thanks a lot for applying some time in order to write “Spring Framework MVC 3.
    1 – Hello World Example”. Thank you once more ,Russ

  • Eric

    Hello Mr Jemmy, can I ask a question?
    Can we use two framework together to develop a web application?
    for example, using .NET framework and MVC framework together to build this “Hello World”?