This tutorial is for the programmers who have little knowledge of Java Spring MVC framework and want to learn this technology from scratch. It shows you how to write Java Spring code from the very beginning.
1.Installation
First, you need to install necessary software to start writing code in Java Spring. You need an IDE to write Java code, a JDK to build your Java code, and a web server to run your code. Also you need a database if your code have database operations. Since Java is a cross platform programming language, you can work under different platforms. In the following, we are using Windows as our development environment.
1.1. JDK
Get the latest Java Platform (JDK) from Oracle (http://www.oracle.com/technetwork/java/javase/downloads/index.html) and install it. After the installation, set JAVA_HOME system environment variable to the JDK installation path.
1.2. Web server (Apache Tomcat)
We use Apache Tomcat as the web server to run our Java Spring code. You can get the latest Apache Tomcat from Apache (http://tomcat.apache.org/) and extract it to a folder. To have a smoke test on the web server, go to the bin folder of the Apache Tomcat installation folder and run startup.bat. Then open a web browser and test the web address http://localhost:8080 to see if you can see the homepage.
1.3. IDE (Spring Tool Suite)
Get the latest Spring Tool Suites from SpringSource (http://www.springsource.org/sts) and install it. The first time you run the Spring Tool Suite, it will ask you to select a workspace. You can input a folder as your workspace and set this folder as the default workspace. In the welcome screen, click Open Dashboard button, and you can start using this nice IDE.
1.4. Database
We can first start development without a database. We will discuss database options in the future articles.
2. Add Apache Tomcat server to Spring Tool Suite
You can run your Java Spring code directly on the web server in the Spring Tool Suite. To achieve that, you first need to add the web server into Spring Tool Suite.
- Right click Servers, and select New -> Others in the context menu. In the pop-up dialog, choose Server and click Next button.
- Choose the Tomcat server name that matches installed version of our web server, e.g. Apache -> Tomcat v7.0 Server, and click Next button.
- Input the Tomcat installation directory, e.g. C:\apache-tomcat-7.0.35, and click Next button.
- In the resources dialog, you can select the project you want to run on the server. Since we don't have any project in the workspace currently, you can click Finish button to finish the process.
- Expand the Servers category in the Package Explorer tab, you can see that there is a new server, Tomcat v7.0 Server at localhost-config, added under this category. Also the Apache Tomcat server is listed in the Servers tab.
3. Run a sample Spring MVC project in Spring Tool Suite
Spring Tool Suite provides a template Spring MVC project for programmers to start with. To open the template project, go to File -> New -> Spring Template Project, and select Spring MVC Project in the pop-up dialog.
In the Project Settings dialog, input the project name and the top-level package name, and click Finish button.
A new project is created and shown in the Package Explorer tab. This template project was configured to build upon JRE 6. If you have a newer version of JRE installed, like what we have with JRE 7, there will be a build error (Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”) and warning (Build path specifies execution environment JavaSE-1.6...) after the project is automatically built.

To fix this problem, right click the project name, and choose the Properties. In the properties dialog, select Java Build Path. In Libraries tab, remove JRE System Library [JavaSE-1.6]. Then click Add Library... button and add JRE System Library. See the following figure on what the project’s Java Build Path should be.
To run the project, right click the project name, Select Run As -> Run on Server. In the Run On Server dialog, choose the Tomcat server we created previously.

After you click Finish button, you now have a Java Spring MVC Hello World application running on your Tomcat web server!

In default, the Hello World application is deployed into the Tomcat web server under the project folder, i.e. $ProjectFolder\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps. You can right click the Tomcat server name under Servers tab, and select Browse Deployment Location... to see the deployment directory.
To deploy the whole web application into our installed Tomcat server, we need to first export the web application into a WAR file, and then deploy the WAR file into the Tomcat server.
To fix this problem, right click the project name, and choose the Properties. In the properties dialog, select Java Build Path. In Libraries tab, remove JRE System Library [JavaSE-1.6]. Then click Add Library... button and add JRE System Library. See the following figure on what the project’s Java Build Path should be.
To run the project, right click the project name, Select Run As -> Run on Server. In the Run On Server dialog, choose the Tomcat server we created previously.
After you click Finish button, you now have a Java Spring MVC Hello World application running on your Tomcat web server!
In default, the Hello World application is deployed into the Tomcat web server under the project folder, i.e. $ProjectFolder\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps. You can right click the Tomcat server name under Servers tab, and select Browse Deployment Location... to see the deployment directory.
To deploy the whole web application into our installed Tomcat server, we need to first export the web application into a WAR file, and then deploy the WAR file into the Tomcat server.
- Right click the Hello project name in the Package Explorer tab, and select Export...
- In the Export dialog, choose web -> WAR file, and click Next button
- Input the web project name and destination (put the file into the webapps folder of the Tomcat installation folder) . You can also check the Optimize for Apache Tomcat v 7.0 option.
- Restart the Tomcat web server and the WAR file should be automatically deployed into the Hello folder. You can visit http://localhost:8080/Hello/ to test the deployment.
4. Summary
In this tutorial, we set up an implementation environment for Java Spring and run a sample Java Spring MVC project on the implementation environment. In the next tutorial, we will introduce some basic concepts of Java Spring MVC framework by analyzing this Hello World sample project and make some changes on the project.


Is there any other way to get answer like this? I tried with out success. Any way thanks for your help.
ReplyDeleteI learned a lot from Besant Technologies in my college days. They are the Best Java Training Institute in Chennai
can you give more details on what did you try without success?
Delete