Spring @MatrixVariable at specific position in a URL

This tutorial will show you how we use @MatrixVariable annotation in Spring Web MVC. The URI has the possibility of including name-value pairs within path segments. The general “URI path parameters” could be applied although the more unique “Matrix URIs”, originating from an old post by Tim Berners-Lee, is also frequently used and fairly well known. Within Spring MVC these are referred to as matrix variables.

Matrix variables can appear in any path segment, each matrix variable separated with a “;” (semicolon). For example: “/cars;color=red;year=2012”. Multiple values may be either “,” (comma) separated “color=red,green,blue” or the variable name may be repeated “color=red;color=green;color=blue”.

If a URL is expected to contain matrix variables, the request mapping pattern must represent them with a URI template. This ensures the request can be matched correctly regardless of whether matrix variables are present or not and in what order they are provided.
Since all path segments may contain matrix variables, in some cases you need to be more specific to identify where the variable is expected to be:

More info can be found in Matrix Variables section in http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html

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.

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

Step 5. Create Controller class

Step 6. Now modify web.xml file

Step 7. Create hello.jsp file under webapp folder

Step 8. Now run the application and hit the URL http://localhost:8080/spring/owners/10;q=52/reports/15;q=25 in the browser, you will see the below output in the browser.

spring matrix variable

Console Output

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.