, ,

Spring Web MVC 4 easy tutorial

July 12, 2014

Spring is a java based framework that has a lot of features to offer, if you’re new to spring go to Spring Wiki. You can visit their documentations here Spring,  I bet you wont read them until you get to feel it first. I’ll create a simple web mvc for you to start with.

1. I’ll be using spring STS Version: 3.5.1.RELEASE, it is built on top of eclipse by the way. Go to File -> New -> Maven Project.

Download Spring STS. What is Maven? I won’t be discussing this tools here, just click the links to know what those are.

start maven


2. Locate your preferred workspace and leave the Create a simple project unchecked then hit next. Then type webapp in the Filter then choose maven-archetype-webapp.

maven webapp archetype


3. Fill in the Group Id, Archetype id and Package. Le’s just say group Id and package are the same and Artifact Id is the name of your project or web app just to keep it simple.

maven arche



4. You have created this project. Notice there is an error saying ‘The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path”. don’t worry we’ll fix this.

Spring MVC project tree

Spring MVC project tree

5. In figure 4 “Spring MVC project tree” you’ll find a file called pom.xml, open that then include this dependency.


6. You should now see your pom.xml like this.

servlet dep


7. Right click the project go to Run As -> Maven install. Your project will update its dependency. Now problem fixed.

mvn install


8. Create java src folder. Right click the project New ->Source Folder

java src


9. Create Controller. Right click the src/main/java then New -> Class. Then enter the package and class name.



10. Now we need to add the spring mvc dependencies in our pom.xml. Spring has a lot of artifacts we only need three dependencies the core, web and webmvc. Add the below dependencies in you pom.xml and Don’t forget to build the project again! Right click the project Run As -> Maven install




It should look like this.

spring deps

By the way, when you run Maven Install, it automatically download the jars and include those in the project, so you won’t need to add them manually.


11. Configure and integrate spring mvc in our web application. Locate the WEB-INF folder.



Right click WEB-INF then New -> Other ->scroll down and look for the XML -> XML File, then click Next. Create an XML file, will just name it spring-mvc-servlet.xml

12. Now we need to place all jsp files in one folder under the WEB-INF as seen in figure 11, create a folder, right click WEB-INF New -> Folder, will just name it pages.



Now we have the spring-mvc-servlet.xml and pages folder.

13. Now we need to tell spring that all transactions in our controllers located in the package com.jabahan.springmvc in step 4, will be forwarded and handled by our jsp located in the pages folder . Open spring-mvc-servlet.xml, then paste this xml.

<beans xmlns="http://www.springframework.org/schema/beans"




<context:component-scan base-package="com.jabahan.springmvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<property name="suffix">

14. Java doesn’t know anything about spring, we need to tell the java web that we are running a spring mvc framework in this application. First we need to tell the container what version, namespaces and schema we are using.

a.) To do this, locate the web.xml as seen in figure 12, open it then add these attributes in your <web-app> element.

<web-app id="SpringMVC" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

b.) Next, paste the below xml inside the <web-app> element





your web.xml should look like this.


15. Create the jsp file. Right click the pages folder we did before, New -> Other -> go to Web folder then select the JSP File, will just name it myfirstcontroller.jsp. Now place this ${name} anywhere in the page you want, will explain this later.

16. Open the controller we created before (Step 9). Paste the below code.

public class HelloController {

public String anymethodname(@RequestParam(value="name")
String name, Model model) {
model.addAttribute("name", name);
return "myfirstcontroller";

Controller should look like this.



RequestMapping /myFirst is the url, RequestParam is the parameter we can send to the controller, spring mvc will send an implicit object Model to our method. Remember the ${name} we added in the jsp? we are mapping the name parameter with the name attribute.

17. Spring STS has already a server we can use. Go to the server window.



18. Locate the server, right click then click Add and Remove.

add remove to server


19. Add the Spring project we just created. Highlight the project click Add > then hit finish.

add remove to server2


20. In the server window hit the start button.

server start


Wait til it finish loading all xmls and jars. Server should have a status of [Started, Synchronized]

21. Go to your favorite browser then hit this URL http://localhost:8080/HelloSpringMVC/myFirst?name=Optimus



Congrats! You just made your first Spring MVC. :)

1 comment

  1. quickfix - July 14, 2014 4:33 pm

    something wrong with the spring-mvc-servlet.xml, for me this one works (tomcat 7): http://pastebin.com/vF9W7Arz


Have your say