Over a million developers have joined DZone.

Tutorial: Your First Simple Spring MVC Template

DZone's Guide to

Tutorial: Your First Simple Spring MVC Template

· Java Zone ·
Free Resource

"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.

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

"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.

java ,frameworks

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}