Spring 3 MVC Hello World Example

Reading Time: 2 minutes

In this simple tutorial I will show you how to create the folder structure, add necessary libraries and write code of Spring 3 MVC. First off all your project has to have the following jars to be imported in your project minimum:

  • antlr-2.7.6.jar
  • commons-lang-2.4.jar
  • commons-logging-1.1.1.jar
  • jstl.jar
  • log4j-1.2.11.jar
  • org.springframework.beans-3.0.6.RELEASE.jar
  • org.springframework.context-3.0.6.RELEASE.jar
  • org.springframework.core-3.0.6.RELEASE.jar
  • org.springframework.expression-3.0.6.RELEASE.jar
  • org.springframework.web-3.0.6.RELEASE.jar
  • org.springframework.web.servlet-3.0.6.RELEASE.jar

This is here how your project folder needs to look like:

Spring3MVCProjectLayout

Add these necessary jars into lib folder, to do it please drag them into the folder so that you won’t have to kill yourself how to. Prohibit yourself from adding the jars as classpath.

So let’s get our hands into codes, section by section I will be giving you the explanations what all the files mean and stand for. First of all create a Dynamic web project with a name called “Spring3MVC” or the way you want. Second of all drag the necessary jars into lib folder and let us get started with the code. First of all create a new folder called “jsp” right under “WEB-INF” folder.

Right click on the project and create a new class called “HelloWorldController.java” and type the package as “com.example.controller”, the below the code comes:

package com.example.controller;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, this is Spring 3.0 MVC!";
        return new ModelAndView("hello", "message", message);
    }
}

This is our controller code, this will be residing in Java Resources src folder which is where it is supposed to be, so let’s move on and create a our jsp files. Right click on the project and create a new jsp file called “index.jsp” under “WebContent” please be aware this index file should not be in “jsp” folder, and paste this code into the file

<html>
<head>
    <title>Spring 3.0 MVC Hello World Sample</title>
</head>
<body>
    <a href="hello.html">Say Hello</a>
</body>
</html>

and now the same way create a new folder in “jsp” folder called “hello.jsp” and paste the code below

<html>
<head>
    <title>Spring 3.0 MVC Hello World Sample</title>
</head>
<body>
    ${message}
</body>
</html>

Its time to create our configuration files which will be in charge of managing our project. First of all let us create the “web.xml” file under “WEB-INF” folder and paste the code below

<?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>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <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>*.html</url-pattern>
    </servlet-mapping>
</web-app>

and now only one file left to be created our spring configuration file called “spring-servlet.xml” please create this folder the same way right underneath the folder “WEB-INF” and paste this code

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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">

    <context:component-scan
        base-package="com.example.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

and now we are pretty much ready to deploy the code to our application server. Please go ahead and give it a shot, and I will below be explaining what these files stand for we newly created.

You can download the full project here Spring3MVC

Leave a Reply

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