From c9acaaf9d8a10d8eceb95ad43af60f7a87f963e3 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 8 Nov 2011 17:49:38 +0000 Subject: [PATCH] SPR-8823 ServletUriComponentsBuilder polish and reference doc update. --- .../WebMvcConfigurationSupport.java | 3 +- .../support}/ServletUriComponentsBuilder.java | 5 +- .../ServletUriComponentsBuilderTests.java | 2 +- spring-framework-reference/src/mvc.xml | 65 ++++++++++++++++++- spring-framework-reference/src/new-in-3.1.xml | 6 ++ 5 files changed, 76 insertions(+), 5 deletions(-) rename {org.springframework.web/src/main/java/org/springframework/web/util => org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support}/ServletUriComponentsBuilder.java (95%) rename {org.springframework.web/src/test/java/org/springframework/web/util => org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support}/ServletUriComponentsBuilderTests.java (98%) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java index ecb6e37c8e..9cd1822f83 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java @@ -356,7 +356,8 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw } /** - * Override this method to add default {@link HttpMessageConverter}s. + * Adds a set of default HttpMessageConverter instances to the given list. + * Subclasses can call this method from {@link #configureMessageConverters(List)}. * @param messageConverters the list to add the default message converters to */ protected final void addDefaultHttpMessageConverters(List> messageConverters) { diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/ServletUriComponentsBuilder.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java similarity index 95% rename from org.springframework.web/src/main/java/org/springframework/web/util/ServletUriComponentsBuilder.java rename to org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java index de6d5ae73f..818653ec7d 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/util/ServletUriComponentsBuilder.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.util; +package org.springframework.web.servlet.support; import javax.servlet.http.HttpServletRequest; @@ -23,6 +23,9 @@ import org.springframework.util.StringUtils; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; +import org.springframework.web.util.UrlPathHelper; /** * A builder for {@link UriComponents} that offers static factory methods to diff --git a/org.springframework.web/src/test/java/org/springframework/web/util/ServletUriComponentsBuilderTests.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java similarity index 98% rename from org.springframework.web/src/test/java/org/springframework/web/util/ServletUriComponentsBuilderTests.java rename to org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java index 7f8c4004b0..cd1798c242 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/util/ServletUriComponentsBuilderTests.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.util; +package org.springframework.web.servlet.support; import static org.junit.Assert.assertEquals; diff --git a/spring-framework-reference/src/mvc.xml b/spring-framework-reference/src/mvc.xml index e821d25ebc..332394ea6e 100644 --- a/spring-framework-reference/src/mvc.xml +++ b/spring-framework-reference/src/mvc.xml @@ -2869,11 +2869,72 @@ public class ContentController { This does not eliminate the possibility of a concurrency issue entirely but nevertheless reduces it greatly with information that is already available in the redirect URL. Therefore the use of flash - attributes is recommended mainly for redirect scenarios unless the - target URL and/or query parameters are known. + attributes is recommended mainly for redirect scenarios . +
+ Building <literal>URI</literal>s + + Spring MVC provides a mechanism for building and encoding a URI + using UriComponentsBuilder and + UriComponents. + + + For example you can expand and encode a URI template string: + +UriComponents uriComponents = + UriComponentsBuilder.fromUriString("http://example.com/hotels/{hotel}/bookings/{booking}").build(); + +URI uri = uriComponents.expand("42", "21").encode().toUri(); + + + Note that UriComponents is immutable and + the expand() and encode() + operations return new instances if necessary. + + You can also expand and encode using individual URI components: + +UriComponents uriComponents = + UriComponentsBuilder.newInstance() + .scheme("http").host("example.com").path("/hotels/{hotel}/bookings/{booking}").build() + .expand("42", "21") + .encode(); + + + In a Servlet environment the + ServletUriComponentsBuilder sub-class provides + static factory methods to copy available URL information from a + Servlet request including host, scheme, port, path and query string: + + +HttpServletRequest request = ... + +ServletUriComponentsBuilder ucb = + ServletUriComponentsBuilder.fromRequest(request).replaceQueryParam("accountId", "{id}").build() + .expand("123") + .encode(); + + + Alternatively, you may choose to copy a subset of the available + information up to and including the context path: + +// Host, port and context path +ServletUriComponentsBuilder ucb = + ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build() + + + Or in cases where the DispatcherServlet is mapped + by name (e.g. /main/*), you can also have the literal part + of the servlet mapping included: + +// Host, port, context path, and the literal part of the servlet mapping +ServletUriComponentsBuilder ucb = + ServletUriComponentsBuilder.fromServletMapping(request).path("/accounts").build() + + +
+
Using locales diff --git a/spring-framework-reference/src/new-in-3.1.xml b/spring-framework-reference/src/new-in-3.1.xml index ed6ea79bf2..3807acab4e 100644 --- a/spring-framework-reference/src/new-in-3.1.xml +++ b/spring-framework-reference/src/new-in-3.1.xml @@ -485,6 +485,12 @@ especially since UriTemplate relies on those same classes internally. + + A ServletUriComponentsBuilder sub-class + provides static factory methods to copy information from + a Servlet request. See . + +