URL mapping with matrix variables

We are now aware of what our user is interested in. It would be a good idea to improve our Tweet controller so that it allows searching from a list of keywords.

One interesting way to pass key-value pairs in a URL is to use a matrix variable. It is pretty similar to request parameters. Consider the following code:

someUrl/param?var1=value1&var2=value2

Instead of the preceding parameter, matrix variables understand this:

someUrl/param;var1=value1;var2=value2

They also allow each parameter to be a list:

someUrl/param;var1=value1,value2;var2=value3,value4

A matrix variable can be mapped to different object types inside a controller:

  • Map<String, List<?>>: This handles multiple variables and multiple values
  • Map<String, ?>

Get Mastering Spring MVC 4 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.