Tutorials Spring Creating Spring MVC HelloWorld Using Maven in Eclipse Tomcat

Creating Spring MVC HelloWorld Using Maven in Eclipse Tomcat

In the course of this tutorial, we will learn how to create your first Spring MVC application using eclipse IDE using Maven build type.

1. Prerequisites

  • Java version 1.7
  • Latest Spring STS
  • Apache Tomcat 7.0.x

This tutorial assumes that, you have downloaded and installed the above mentioned softwares on your computer. If you have not installed Java on your development machine, you can follow the official oracle documentation for the installation for your configuration.

Without spending any further time, let us jump straight into creating the project in eclipse.

1. Create a Maven Project

  • File -> New -> Other… It will open the eclipse select wizard.

Create Maven Spring project

  • Select Maven -> Maven Project and click Next.
  • Again click Next on New Maven Project dialog.
  • Select an Archetype either by filtering webapp or selecting from the available artifacts and click Next.

Create new eclipse maven project

  • Provide the details for Group Id and Artifact Id and click Finish. As a general practice, the group id is your company domain name (i.e. com.javatechig) and artifact id is the name of your application.
  • This will create a basic Maven project template in eclipse.

3. Create a Server Instance

In this example we will be using Apache Tomcat application server for deploying our Spring MVC application. Now let us add a server instance on eclipse.

  • File -> New -> Other… It will open the eclipse select wizard.

Add Tomcat Server Eclipse

  • Select Server -> Server and click Next.
  • Select Apache -> Tomcat v7.0 Server and click Next. In this example, we will be using Apache Tomcat version 7.0. However, the similar steps will work for other tomcat versions.
  • Browse and select the Apache Tomcat v7.0 server installation directory and click Finish.

4. Update Project Build Path

You might notice a error on your project. This is due to the project Build path problem. To fix this you need to right click on project -> Properties to open project Java Build Path settings.

Select Libraries tab and click on Add Library…-> Server Runtime -> Apache Tomcat -> Finish.

5. Configure Spring Dependency

Before we develop Spring MVC web application, we need to configure the project by adding the required Maven dependencies. Add the following Maven dependencies into your pom.xml file.

pom.xml

    4.0.0
    com.javatechig
    HelloMVC
    war
    0.0.1-SNAPSHOT
    HelloMVC Maven Webapp
    http://maven.apache.org

    
    
        4.2.1.RELEASE
    

    

        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
            org.springframework
            spring-web
            ${spring.version}
        
        

        
            junit
            junit
            3.8.1
            test
        
    
    
        HelloMVC
    

With this the basic Spring project configuration is complete. Your project structure should look like as the following screenshot.

Spring MVC Eclipse Maven Project Structure

The following steps will take you through the rest of the steps to create and deploy Spring MVC HelloWorld app.

6. Configure Dispatcher Servlet

The DispatcherServlet must be configured as normal in web.xml to bootstrap a Spring WebApplicationContext. Edit the default web.xml file and add the following.

web.xml



    Archetype Created Web Application
    
        contextConfigLocation
        /WEB-INF/dispatcher-servlet.xml
    

    
        
            org.springframework.web.context.ContextLoaderListener
        
    

    
        dispatcher
        
            org.springframework.web.servlet.DispatcherServlet
        
        1
    

    
        dispatcher
        /
    

7. Mapping Requests

Create an xml file dispatcher-servlet.xml under the same directory of web.xml.

dispatcher-servlet.xml


    

    
        
            /WEB-INF/views/
        
        
            .jsp
        
    

In the above xml file, base-package specifies the package of the controllers. prefix specifies the directory of views, and it is set to be /WEB-INF/views/, which means views directory should be created under WEB-INF. suffix specifies the file extension of views. For example, given a view hello, the view will be located as /WEB-INF/views/hello.jsp.

8. Create Controller class

Create the HelloWorldController under src/main/java/ directory.

HelloWorldController.java
package com.javatechig.controller;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView welcomeMessage(
            @RequestParam(value = "name", required = false) String name) {
        // Name of your jsp file as parameter
        ModelAndView view = new ModelAndView("hello");
        view.addObject("name", name);
        return view;
    }
}

In the code above, @RequestMapping annotation maps web requests onto specific handler classes and/or handler methods, in this case, welcomeMessage(). It provides a consistent style between Servlet environments, with the semantics adapting to the concrete environment.

RequestParam indicates that a method parameter should be bound to a web request parameter. In this case, we also make it not required and give it a default value. The ModelAndView(“hello”) determines that hello is the target view.

9. Working with Views

Edit the default index.jsp and add the following code snippets.

index.jsp


    

Hello World

Click here...

Add a JSP files hello.jsp file under /WEB-INF/views/ directory.

hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>




Spring 4 MVC
<%@ page isELIgnored="false"%>


    

Hello, ${name}. Welcome to Spring MVC!

Now we are done with our first HelloWorld example. To deploy it on Tomcat application server, Right click on the project -> select Run as. Choose Tomcat server, select Next and Finish. It will deploy the project and to see the output visit http://localhost:8080/HelloMVC on your browser. you will see the following output on your browser.

Spring MVC HelloWorld usign Eclipse Maven2

Spring MVC HelloWorld usign Eclipse Maven

Total
0
Shares
guest
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
javalschool
javalschool
7 years ago

very good tutorial sir clean explanation thank u sir

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago
Reply to  javalschool

🙂

Patient Baïse
Patient Baïse
6 years ago

Hi, Nilanchala Panigrahy
can you help me?
my email is: patientbaise@gmail.com

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago
Reply to  Patient Baïse

How can I help you ?

Patient Baïse
Patient Baïse
6 years ago

i have a projet on Rss Reader but i can’t have image with glide

the example of my projet : http://android-pratap.blogspot.com/2015/02/simple-rss-reader-application-in-android.html

Akshay Taru
Akshay Taru
6 years ago

Some things are missing like not getting “project -> select Run as. Choose Tomcat server” option and whether to create package id, I also feel that the screen shot showing folder structure should be updated. Showing each and every file.

kinsley kajiva
kinsley kajiva
6 years ago

hi, please can you do a tutorial on pre-fetching images into glide cache for android in the background thread or service intent

Esmirna Astorga
Esmirna Astorga
6 years ago

Dear,

Can you explain me how to put an image as a logo or as a background? Because I got a mapping error with a simple call

Regards

prashant
prashant
6 years ago

HI Nilanchala,
This solution really helpful.
Thank you so much for posting!

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago
Reply to  prashant

Thats great. Thanks for your good words.

Previous Post

Introduction to Spring MVC and Dispatcher Servlet

Next Post

How Spring Controller Request Mapping works in Spring MVC

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
10
0
Would love your thoughts, please comment.x
()
x