Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some information about @MatrixVariable annotation ins Spring 3.2

today I am studying the Spring MVC showcase dowlodable from the STS dashboard

I have some doubt about the new annotation @MatrixVariable introduced by Spring 3.2 version and the use of the Matrix variables in the URI path.

Within my home.jsp view I have the following link:

<a id="matrixVar" class="textLink" href="<c:url value="/data/matrixvars;foo=bar/simple" />">Matrix variable</a>

Reading here the official documentation about @MatrixVariable annotation: http://static.springsource.org/spring-framework/docs/3.2.0.M2/reference/html/mvc.html#mvc-ann-matrix-variables

I have understand the following thing:

1) Matrix variables are a couple 2) I can put these variable in any URI path segment

So I have some doubts about the previous link:

  1. What is the URI called when I click on the previus link?

    Is it /spring-mvc-showcase/data/matrixvars/simple ?!?! (with spring-mvc-showcase that is only the application name)

  2. It would have been indifferent if I had put the "foo" matrix variable in some other position?

For example if I put the "foo" variabile in this way:

"/data;foo=bar/matrixvars/simple"

or in this way:

"/data/matrixvars/simple;foo=bar"

Has it the same meaning?

Ok, now this is the controller method that handles the HTTP Request generated by clicking the link:

@RequestMapping(value="{path}/simple", method=RequestMethod.GET)
public @ResponseBody String withMatrixVariable(@PathVariable String path, @MatrixVariable String foo) {
    return "Obtained matrix variable 'foo=" + foo + "' from path segment '" + path + "'";
}

This method use the URI Template pattern to accede to a specific position of the URI.

So if my URI is data/matrixvars/simple and because my entire class is annoted by @RequestMapping("/data") annotation and my controller method is annoted by: @RequestMapping(value="{path}/simple", method=RequestMethod.GET) that means the following thing?

the path URI Template variable is related to the second URI position (that in the current URI is matrixvars) and so this method can handle also URI like:

/spring-mvc-showcase/data/SOMETHING/simple 

Is it right?

Thank you very much

Andrea

like image 600
AndreaNobili Avatar asked Nov 13 '22 15:11

AndreaNobili


1 Answers

Answers:

What is the URI called when I click on the [ /data/matrixvars;foo=bar/simple ] link?

  • From the browser's point of view, the URI is exactly as given.
  • In Spring MVC, it is /data/matrixvars/simple; the actual matrix variables "disappear" from the URI since matrix variables are considered optional components of the URI.

It would have been indifferent if I had put the "foo" matrix variable in some other position?

  • NO. Matrix variables are tied to their position in the path. If you move them to a different path position then they change their meaning.

... so this method can handle also URI like /spring-mvc-showcase/data/SOMETHING/simple

  • Correct, with your foo MatrixVariable as defined supporting something like /spring-mvc-showcase/data/SOMETHING;foo=bar/simple
like image 70
William Price Avatar answered Nov 15 '22 05:11

William Price