Spring MVC URI Template (3.0)

Spring MVC URI template (in version 3.0) is a string containing one or more variables or place holders which can be replaced to generate URL out of this. Though this feature appears simple, but uses of this can be significant. Suppose we want to have valid a user id or a session id with each in coming request. Instead of retrieving this attribute from request or session object, we receive as a url variable. Let us see how this works.

Spring MVC URI Template Classes:

http://localhost:8080/getCustomerDetails -> CustomerDetailsController

The mandatory input parameter in this case will be customer id. Any request that comes without customer id is meaningless. If we make customer id a part of accessing url then the controller will be hit if there is customer id value in url.

http://localhost:8080/getCustomerDetails/{customerId}  -> CustomerDetailsController

http://localhost:8080/getCustomerDetails/1234 -> CustomerDetailsController

This feature is now available with Spring MVC 3.0. In this tutorial, we are going to write a simple application that uses this feature. I am taking code base from previous post on Spring MVC 3.0 and upgrading it to use URI-template feature.

Here is the list of code elements we modify or add on top of above mentioned tutorial code.

–          HelloWorldController.java (modify)

–          SpringMVC3Tutorial-servlet.xml (modify)

–          web.xml (modify)

–          jstl.jar (add)

–          helloworld.jsp (no change from previous post)

Let us see each code element and understand what special we do here.


package com.myorg.springmvctutorial.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HelloWorldController{
	protected final Log logger = LogFactory.getLog(getClass());

	@RequestMapping(value="/helloWorld/{id}", method=RequestMethod.GET)
	public ModelAndView helloWorld(@PathVariable String id, Model model ){
		logger.info("id " + id);
		return new ModelAndView("helloworld");

We have made following changes in this controller class.

–          RequestMapping annotation contains variable {id} that is the URI-template variable.

–          Annotation @PathVariable specifying input to the request handler method.

–          Return value changed from ModelAndView(“helloworld.jsp”) to ModelAndView(“helloworld”).


<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:p=”http://www.springframework.org/schema/p”
<context:component-scan base-package=”com.myorg.springmvctutorial.web.controller” />
<bean id=”viewResolver”
<property name=”viewClass”
value=”org.springframework.web.servlet.view.JstlView” />
<property name=”prefix” value=”/” />
<property name=”suffix” value=”.jsp” />

In controller class, the return value does not contain .jsp postfix to the ModelAndView input string. We use view resolver to do it for us. I tried making it work without view resolver but it does not reach to correct view.


<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
id=”WebApp_ID” version=”2.5″>

Here the url-pattern is changed from *.htm to /. This is second place where code using annotation PathVariable does not work. Previous pattern *.htm did not work with PathVariable. Because of this now our url will be http://localhost:8080/SpringMVC3Tutorial/helloWorld/abc where abc is the id value.


We require this jar for view resolver bean entry.

Make these changes, deploy application on tomcat as a web archive (or the way you like), and this url will show you the jsp displayed correctly.


Spring MVC Documentation


  1. Thanks for the tutorial.
    I have a problem, in my case the value that pass as parameter has a “.” in it. Lile “LastName.FirstName”. In my controller, I only get the LastName part, the rest was gone. Any idea why that happened?


  2. Is there any way I can have both annotation based url mapper and xml beans based url mappers. And for that purpose do I need to have 2 servlets ? one with url-pattern as /* and other with *.htm

Leave a Reply

Your email address will not be published.