01: Spring MVC beginner tutorial step by step

This tutorial extends Getting started with Apache Tomcat application server by deploying a simple JEE Application to introduce Spring MVC.

Spring MVC project structure

Spring dependencies via Maven

Step 1: Modify the pom.xml file in the simpleWeb project to bring in the Spring web dependency jars.

Define a controller & store a model value with a key say “modelValue”

Step 2: Create a base package under src/main/java for the the controllers “com.mytutorial.controller

Step 3: Create a controller named “SimpleController” in the package “com.mytutorial.controller”.

Define a view & extract the model value “modelValue”

Step 4: Create a folder named “views” for the JSP page views in the “src/main/webapp/WEB-INF” folder.

Step 5: Create a JSP page “src/main/webapp/WEB-INF/views/output.jsp“.

Wire up the controller & view

Step 6: Define the spring beans config xml file “applicationContext.xml” in the folder src/main/webapp/WEB-INF.

Step 7: Define the deployment descriptor file “web.xml” in the folder “src/main/webapp/WEB-INF“.

Build the simpleWeb.war

Step 8: Use the “mvn clean install” from a DOS console from the folder “c:\projects\simpleWeb” or from within maven by right mouse clicking on the project “simpleWeb” and then select “Run As –> Maven Install

Deploy simpleWeb.war to Tomcat & start the server

Step 9: Copy the “C:\projects\simpleWeb\target\simpleWeb.war” to the tomcat installation folder “C:\tools\apache-tomcat-9.0.0.M21-windows-x64\apache-tomcat-9.0.0.M21\webapps

Step 10: Start the Tomcat server

Access the URLs in a browser

1) http://localhost:8080/simpleWeb/entry/myc/

2) http://localhost:8080/simpleWeb/entry/myc/simpleAgain

Spring MVC Flow

Spring MVC Flow


800+ Java Interview Q&As Menu

Top