Spring 4 MVC Hello World Simple Url Handler Mapping Example

July 17, 2015 | Updated last October, 2015 | 1567 views | Comments

Using the SimpleUrlHandlerMapping Class of Spring WebMVC Framework 4, a simple "Hello World" web application example can be created.

 

Built and tested with the following:

  • Eclipse Juno IDE
  • Apache Tomcat 7.0.47
  • JDK 1.7.0
  • Maven 3.0.4
  • Spring Web-MVC Framework 4.2.2
    • Configuration: XML
    • Url Mapping: Simple Url Handler

 

Step 1. Create a new Maven Project.

Set up a new Maven Project with the following details:

  • Group Id: com.consistentcoder
  • Artifact Id: spring-4-mvc-hello-world-simple-url-handler-mapping
  • Name: spring 4 mvc hello world simple url handler mapping

Tutorials:
Create a New Maven Project in Eclipse
Configure Run on Server option on a Maven Project on Eclipse IDE

 

Step 2. Edit pom.xml file.

Configure details needed to build the project together with the project information.

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.consistentcoder</groupId>
    <artifactId>spring-4-mvc-hello-world-simple-url-handler-mapping</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>spring 4 mvc hello world simple url handler mapping</name>
    
    <dependencies>
        <!-- Spring Framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>
        
        <!-- Java Servlet API -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</project>

/pom.xml

 

Step 3. Edit web.xml file.

Map all incoming request to the "DispatcherServlet" which will be declared on the "Deployment Descriptor".

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>spring-4-mvc-hello-world-simple-url-handler-mapping</display-name>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

/src/main/webapp/WEB-INF/web.xml

 

Step 4. Create spring-servlet.xml file.

Find corresponding controller of an incoming request which will create a model that will return the view file.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-4.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
  
    <!-- SimpleUrlHandlerMapping -->
    <bean id="simpleUrlHandlerMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /=simpleController
            </value>
        </property>
    </bean>
    <bean name="simpleController" class="com.consistentcoder.controllers.Main" />
  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

/src/main/webapp/WEB-INF/spring-servlet.xml

 

Step 5. Create the template file.

Create another folder inside "WEB-INF" and name it "jsp". Create a new jsp file inside it and name it "hello.jsp", then put the following code inside it.

<%@ 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>Spring 4 MVC Hello World Simple Url Handler Mapping Example</title>
</head>
<body>
    <h1>${ message }</h1>
</body>
</html>

/src/main/webapp/WEB-INF/jsp/hello.jsp

 

Step 6. Create the Controller Class.

Interpret and transform incoming request into a model. Create a new package and name it "com.consistentcoder.controllers". Create a new class inside it, name the class "Main".

Tutorials:
Create a New Package on Eclipse IDE
Create a New JAVA Class on Eclipse IDE

package com.consistentcoder.controllers;

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 Main extends AbstractController {
    public ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
        ModelAndView model = new ModelAndView();
        model.addObject("message", "Spring 4 MVC Hello World<br>Simple Url Handler Mapping Example");
        model.setViewName("hello");
        return model;
    }
}

/src/main/java/com/consistentcoder/constrollers/Main.java

 

That's all!

Spring 4 MVC Hello World Simple Url Handler Mapping Example has been developed.

 

Final Project Structure

 

What's Next? Test the application.

Start your server and browse this link. http://localhost:8080/spring-4-mvc-hello-world-simple-url-handler-mapping/