Add support for matrix variables

A new @MatrixVariable annotation allows injecting matrix variables
into @RequestMapping methods. The matrix variables may appear in any
path segment and should be wrapped in a URI template for request
mapping purposes to ensure request matching is not affected by the
order or the presence/absence of such variables. The @MatrixVariable
annotation has an optional "pathVar" attribute that can be used to
refer to the URI template where a matrix variable is located.

Previously, ";" (semicolon) delimited content was removed from the
path used for request mapping purposes. To preserve backwards
compatibility that continues to be the case (except for the MVC
namespace and Java config) and may be changed by setting the
"removeSemicolonContent" property of RequestMappingHandlerMapping to
"false". Applications using the  MVC namespace and Java config do not
need to do anything further to extract and use matrix variables.

Issue: SPR-5499, SPR-7818
This commit is contained in:
Rossen Stoyanchev
2012-08-26 16:22:37 -04:00
parent da05b094f5
commit 2201dd8c45
29 changed files with 1392 additions and 116 deletions

View File

@@ -16,11 +16,14 @@
package org.springframework.web.util;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.UnsupportedEncodingException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
/**
@@ -79,10 +82,32 @@ public class UrlPathHelperTests {
}
@Test
public void getRequestRemoveSemicolonContent() throws UnsupportedEncodingException {
helper.setRemoveSemicolonContent(true);
request.setRequestURI("/foo;f=F;o=O;o=O/bar;b=B;a=A;r=R");
assertEquals("/foo/bar", helper.getRequestUri(request));
}
@Test
public void getRequestKeepSemicolonContent() throws UnsupportedEncodingException {
helper.setRemoveSemicolonContent(false);
request.setRequestURI("/foo;a=b;c=d");
assertEquals("/foo;a=b;c=d", helper.getRequestUri(request));
request.setRequestURI("/foo;jsessionid=c0o7fszeb1");
assertEquals("jsessionid should always be removed", "/foo", helper.getRequestUri(request));
request.setRequestURI("/foo;a=b;jsessionid=c0o7fszeb1;c=d");
assertEquals("jsessionid should always be removed", "/foo;a=b;c=d", helper.getRequestUri(request));
}
//
// suite of tests root requests for default servlets (SRV 11.2) on Websphere vs Tomcat and other containers
// see: http://jira.springframework.org/browse/SPR-7064
//
//
//
@@ -297,7 +322,7 @@ public class UrlPathHelperTests {
request.setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, "original=on");
assertEquals("original=on", this.helper.getOriginatingQueryString(request));
}
@Test
public void getOriginatingQueryStringNotPresent() {
request.setQueryString("forward=true");

View File

@@ -16,16 +16,19 @@
package org.springframework.web.util;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.util.MultiValueMap;
/**
* @author Juergen Hoeller
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public class WebUtilsTests {
@@ -64,4 +67,34 @@ public class WebUtilsTests {
assertEquals("view.html", WebUtils.extractFullFilenameFromUrlPath("/products/view.html?param=/path/a.do"));
}
@Test
public void parseMatrixVariablesString() {
MultiValueMap<String, String> variables;
variables = WebUtils.parseMatrixVariables(null);
assertEquals(0, variables.size());
variables = WebUtils.parseMatrixVariables("year");
assertEquals(1, variables.size());
assertEquals("", variables.getFirst("year"));
variables = WebUtils.parseMatrixVariables("year=2012");
assertEquals(1, variables.size());
assertEquals("2012", variables.getFirst("year"));
variables = WebUtils.parseMatrixVariables("year=2012;colors=red,blue,green");
assertEquals(2, variables.size());
assertEquals(Arrays.asList("red", "blue", "green"), variables.get("colors"));
assertEquals("2012", variables.getFirst("year"));
variables = WebUtils.parseMatrixVariables(";year=2012;colors=red,blue,green;");
assertEquals(2, variables.size());
assertEquals(Arrays.asList("red", "blue", "green"), variables.get("colors"));
assertEquals("2012", variables.getFirst("year"));
variables = WebUtils.parseMatrixVariables("colors=red;colors=blue;colors=green");
assertEquals(1, variables.size());
assertEquals(Arrays.asList("red", "blue", "green"), variables.get("colors"));
}
}