I'm Benedict Aluan, junior software developer from Philippines. Benedict has posted 1 posts at DZone. View Full User Profile

Tutorial: Your First Simple Spring MVC Template

  • submit to reddit

In this post, I'm going to show you how to create your first simple Spring MVC template. Before we start, I assume that you already know the basics of Spring MVC. If you don't, I suggest to read first some introductions to Spring. Below are some good resources to start.

Let's start. Setup a new blank project. Set all API dependencies in your classpath then configure your web.xml like this:

<WEB-APP xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" schemalocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi="http://www.w3.org/2001/XMLSchema-instance">






I set two mappings, one for the url pattern and the other one is for the welcome page. Next is setting-up the beans. In your myproject-servlet.xml, put these configurations:

<BEANS xmlns="http://www.springframework.org/schema/beans" schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" xsi="http://www.w3.org/2001/XMLSchema-instance">

<BEAN class=com.myproject.controllers.PageController name="myProjectController">
<property name="methodNameResolver" ref="actionMethodNameResolver"> </property>

<BEAN class=org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver id=actionMethodNameResolver>
<property name="mappings">
  <PROP key="/index.html">showIndex</PROP> 

<BEAN class=org.springframework.web.servlet.handler.SimpleUrlHandlerMapping id=simpleUrlMapping>
<property name="mappings">
     <PROP key="*.html">myProjectController</PROP>

<BEAN class=org.springframework.web.servlet.view.ResourceBundleViewResolver id=viewResolver>
<property name="basename" value="views"></property>

 I used the ResourceBundleViewResolver to configure the views using properties file in order for us to still edit the configurations of views without recompiling the project. The bean actionMethodNameResolver is where all your url requests will be dispatched to its proper controller. Since the prop /index.html has a value of showIndex, Your PageController should contain this method:

package com.myproject.controllers;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PageController extends MultiActionController {

public ModelAndView showIndex(HttpServletRequest request, HttpServletResponse response) {
return new ModelAndView(index-page, "model", "Hello World!");

Next step is to create the view index-page. Create a properties file named views. It should contain the class and the url of your jsp. Take note that you need the jstl lib in your classpath.


Create the index.jsp under WEB-INF. To get the model from our controller, we can use this code.

Set up your Tomcat server. Deploy and run the project. Access your local server (http://localhost:[port]) and your done. You should see the "Hello World!". You can now add other page requests on your actionMethodNameResolver's props and configure the proper method in your PageController. Don't forget to add your views configurations in the views.properties. You can download the source code here. I created the project using Idea IntelliJ and included the required libraries so you don't have to download them. You can also import the project using Apache Ant. If you have questions, post in comments.

From http://benpad.blogspot.com

Published at DZone with permission of its author, Benedict Aluan.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Alessandro Santini replied on Tue, 2009/03/10 - 4:06am

I am sorry for being rude but seriously - do we need another article like this?

Benedict Aluan replied on Tue, 2009/03/10 - 4:30am in response to: Alessandro Santini

People who are new to Spring needs this.

James Sugrue replied on Tue, 2009/03/10 - 4:49am in response to: Alessandro Santini

I believe we do - it's good, concise introductory tutorial to Spring MVC. Definitely useful for beginners and as a quick reference. James

Slava Lo replied on Tue, 2009/03/10 - 5:00am

 Annotation-based configuration of mvc components is missing  in this artical, worthwhile for those who is considering Spring 3.

Pay special attention to the following list of deprecated classes in spring 3, especially those from the MVC framework (majority of abstract controllers): http://static.springsource.org/spring/docs/3.0.x/javadoc-api/deprecated-list.html.



Andy Leung replied on Tue, 2009/03/10 - 8:07am

I wish you posted this 2 years ago when I was struggling with Spring's document to just get a my simple Spring MVC webapp up and running!!! Good job! It is a very good simple guide for beginners.

Raghunath Reddy... replied on Fri, 2009/03/13 - 2:21am

Hi it seems I am the right person to get helped with this article..Please guide me what to do for this - "Set all API dependencies in your classpath "

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.