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.

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.

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 ApacheTomcat 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.

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.

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.

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.

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.

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.

500

5Comment threads

5Thread replies

0Followers

Most reacted comment

Hottest comment thread

500

newestoldestmost voted

prashant

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

Vote Up0Vote Down Reply

Aug 9, 2016 11:52 AM

Nilanchala Panigrahy

Thats great. Thanks for your good words.

Vote Up0Vote Down Reply

Aug 12, 2016 5:20 PM

Esmirna Astorga

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

Vote Up0Vote Down Reply

May 3, 2016 12:15 AM

kinsley kajiva

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

Vote Up0Vote Down Reply

Apr 17, 2016 12:03 PM

Akshay Taru

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.