This tutorial will show you how we use @PathVariable annotation with Regular Expressions in Spring Web MVC. Sometimes we need more precision in defining URI template variables. The @RequestMapping annotation supports the use of regular expressions in URI template variables. The syntax is {varName:regex} where the first part defines the variable name and the second – the regular expression.

For example:

    public void handle(@PathVariable String fileName, @PathVariable String version, @PathVariable String extension) {
        // ...

For this tutorial we will create maven based web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

The following configurations are required in order to run the application
JDK 1.7
Tomcat 7
Have maven installed and configured
Spring 4 and JSTL API dependencies in pom.xml
Now we will see the below steps how to create a maven based spring project in Eclipse
Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : spring
Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""
        <!-- spring-context which provides core functionality -->
        <!-- The spring-aop module provides an AOP(aspect-oriented programming)
            implementation -->
        <!-- The spring-webmvc module contains Spring’s model-view-controller (MVC)
            and REST Web Services implementation for web applications -->
        <!-- The spring-web module provides basic web-oriented integration features
            such as multipart file upload functionality and the initialization of the
            IoC container using Servlet listeners and a web-oriented application context -->
        <!-- jstl -->

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.

Step 4. Create Spring XML configuration file controllers.xml under src/main/resources/spring directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:p=""
    <!-- Support annotation -->
    <!-- Scan the package where Spring Controllers are placed -->
    <context:component-scan base-package="com.roytuts.spring.controllers" />
    <!-- Resolves logical String-based view names to actual View types -->
    <bean id="viewResolver"
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- Where pages are kept -->
        <property name="prefix" value="/" />
        <!-- What is the page extension -->
        <property name="suffix" value=".jsp" />

Step 5. Create Controller class

package com.roytuts.spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
public class HelloWorldController {
  @RequestMapping(value = "helloWorld/{fileName:[a-z-]+}-{version:\d\.\d\.\d}{extension:\.[a-z]+}")
  public String helloWorld(@PathVariable String fileName, @PathVariable String version, @PathVariable String extension, Model model) {
    model.addAttribute("fileName", fileName + version + extension);
    return "hello";

Step 6. Now modify web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi=""
    <display-name>Spring MVC</display-name>
    <!-- dispatcher servlet acts as a front controller for each request/response -->
        <!-- load Spring controllers while dispatcher servlet loads -->

Step 7. Create hello.jsp file under webapp folder

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    <strong>@PathVariable</strong> with <strong>Regular Expression value : </strong> ${fileName}

Step 8. Now run the application you will see the below output in the browser.

URI template with regular expression

Thanks for reading.