What's Next: View All Tutorial →

Spring MVC Hello World

A simple program to display "hello world" in the web page   Technology used in the Article: 1. Spring 4 2. Maven 3. Netbean 8 IDE Note: The Maven plugin will come together with the Netbean IDE, There is no additio...

Published Date: 13/12/2014  Last Modified Date: 13/12/2014    New Post

A simple program to display "hello world" in the web page


Technology used in the Article:

1. Spring 4
2. Maven
3. Netbean 8 IDE

Note: The Maven plugin will come together with the Netbean IDE, There is no additional configuration required for maven to setup in Netbeans IDE. Maven is used only for downloading the required spring jar to the project.

Netbean IDE is similar to eclipse IDE. To use the same short key as eclipse in Netbean IDE, Go to Tools -> option -> Keymap -> Profile - >Eclipse

1. Create a new Maven Web application in Netbeans IDE

Go to File -> New Project -> Maven -> Web Application
Enter the Project Name: SpringMVC
GroupId: com.tutorial
Click "Next" and select the server(Either Glassfish or Tomcat)
Click "Finish" to create a project

2. Add Spring library to download required spring jar using maven

Open the pom.xml file, 

Add the spring-context dependency

<dependecy> should be added inside the <dependencies>

3. Create the resources folder for Maven Projects

The resouces folder will not be created by default, To create the resource folder

Go to NetBeans Editor -> Windows -> Files

The folder view will be opened. Expand the folder src and main.

Create a new folder "resources" under the folder main.

After the resources folder created Go to the projects view, and you can see "Others sources (src/main/resources)" 

Alternatively, we can create the resources folder in path "src/main/resources" where the project is created.

4. Create web.xml file and add Initialize SpringDispatcher Class

Create a folder "WEB-INF" in the "Web pages" if the "WEB-INF" folder does not exits

Similarly, Create a file "web.xml" in the path "Web pages/WEB-INF"

Web Pages relates to (src/main/webapp) in folder structure

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="true" >
    <!-- Start of spring web app initialize-->
        <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>                        
    <!-- End of spring web app initialize-->

5. Create dispatch-servlet xml file to provide the Spring MVC controller settings

dispatcher-servlet.xml (Path: Web pages/WEB-INF/dispatcher-servlet.xml)
<beans xmlns="http://www.springframework.org/schema/beans"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:annotation-driven  />
    <context:component-scan base-package="com.tutorial.web" />    
        <property name="prefix">
        <property name="suffix">
InternalResourceViewResolver   maps the jsp page  that controller class return.

for Example: If the controller return "helloworld", then the file store in the path /WEB-INF/pages/helloworld.jsp" will be executed.

<context:component-scan>  will scan all the class anotated with @Controller anotation from the given package

<mvc:annotation-driven>   need to be added to provide support for annotation tag

6. Spring Web Controller (Java class)

Create a new Package "com.tutorial.web" and create a classHelloWorldController.java 
package com.tutorial.web;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

public class HelloWorldController {

    public String home(ModelMap map, HttpServletRequest request) {

        //ModelMap is an object that send the object to the JSP page.
        //N number of item can be added and retrieve using Key,Value pairs,
        //It is like java.util.Map object
        map.addAttribute("greetingText", "Hi Merchant,");
        map.addAttribute("welcomeText", "Happy Catching. Register your shop and sell your product online");

        //return value is the jsp file name. the folder path will be mapped in the spring-dispatcher.xml using InternalResourceViewResolver bean
        return "homepage";

@Controller  - to be mention for the Controller file 
@RequestMapping - to create the request url. When the browser hit this url, this function will be called and executed.
ModelMap - ModelMap is an object that send the data to the JSP page. N number of data can be added and retrieve using Key,Value pairs, It is like java.util.Map object


7. Helloword.jsp - output page

Create new file helloworld.jsp in the pages folder
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring MVC Helloworld</title>

8. Program Execution

Right click the Project and click "Run" to run the project

Hit the url http://localhost:8084/SpringMVC/helloworld in the webpage, the dispatcher will be intialize in the console
org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started
org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sat Dec 13 12:11:09 IST 2014]; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod Mapped "{[/helloworld],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.tutorial.web.HelloWorldController.home(org.springframework.ui.ModelMap,javax.servlet.http.HttpServletRequest)
org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization completed in 3789 ms

//Output in the webpage

Hi Merchant,

Happy Catching. Register your shop and sell your product online


9. Download Project

Download project from google drive(7 KB)
Awaiting for Administrator approval

Tags: [28]

← Back to list

Related Post