diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index 1685db1b66..da57576b6d 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -253,7 +253,7 @@ public class UriComponentsBuilder { // build methods /** - * Builds a {@code UriComponents} instance from the various components contained in this builder. + * Build a {@code UriComponents} instance from the various components contained in this builder. * @return the URI components */ public UriComponents build() { @@ -261,10 +261,10 @@ public class UriComponentsBuilder { } /** - * Builds a {@code UriComponents} instance from the various components + * Build a {@code UriComponents} instance from the various components * contained in this builder. * @param encoded whether all the components set in this builder are - * encoded ({@code true}) or not ({@code false}). + * encoded ({@code true}) or not ({@code false}). * @return the URI components */ public UriComponents build(boolean encoded) { @@ -278,8 +278,8 @@ public class UriComponentsBuilder { } /** - * Builds a {@code UriComponents} instance and replaces URI template variables - * with the values from a map. This is a shortcut method, which combines + * Build a {@code UriComponents} instance and replaces URI template variables + * with the values from a map. This is a shortcut method which combines * calls to {@link #build()} and then {@link UriComponents#expand(Map)}. * @param uriVariables the map of URI variables * @return the URI components with expanded values @@ -289,8 +289,8 @@ public class UriComponentsBuilder { } /** - * Builds a {@code UriComponents} instance and replaces URI template variables - * with the values from an array. This is a shortcut method, which combines + * Build a {@code UriComponents} instance and replaces URI template variables + * with the values from an array. This is a shortcut method which combines * calls to {@link #build()} and then {@link UriComponents#expand(Object...)}. * @param uriVariableValues URI variable values * @return the URI components with expanded values @@ -299,6 +299,16 @@ public class UriComponentsBuilder { return build(false).expand(uriVariableValues); } + /** + * Build a URI String. This is a shortcut method which combines calls + * to {@link #build()}, then {@link UriComponents#encode()} and finally + * {@link UriComponents#toUriString()}. + * @see UriComponents#toUriString() + */ + public String toUriString() { + return build(false).encode().toUriString(); + } + // URI components methods diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java index d6a9039806..ef02c05916 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java @@ -97,9 +97,8 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * Create a {@link UriComponentsBuilder} from the mapping of a controller class * and current request information including Servlet mapping. If the controller * contains multiple mappings, only the first one is used. - * * @param controllerType the controller to build a URI for - * @return a UriComponentsBuilder instance, never {@code null} + * @return a UriComponentsBuilder instance (never {@code null}) */ public static UriComponentsBuilder fromController(Class controllerType) { String mapping = getTypeRequestMapping(controllerType); @@ -124,14 +123,11 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * Create a {@link UriComponentsBuilder} from the mapping of a controller method * and an array of method argument values. This method delegates to * {@link #fromMethod(java.lang.reflect.Method, Object...)}. - * * @param controllerType the controller * @param methodName the method name * @param argumentValues the argument values * @return a UriComponentsBuilder instance, never {@code null} - * - * @throws java.lang.IllegalStateException if there is no matching or more than - * one matching method. + * @throws IllegalStateException if there is no matching or more than one matching method */ public static UriComponentsBuilder fromMethodName(Class controllerType, String methodName, Object... argumentValues) { Method method = getMethod(controllerType, methodName, argumentValues); @@ -160,8 +156,7 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * Create a {@link UriComponentsBuilder} by invoking a "mock" controller method. * The controller method and the supplied argument values are then used to * delegate to {@link #fromMethod(java.lang.reflect.Method, Object...)}. - *

- * For example given this controller: + *

For example, given this controller: *

 	 * @RequestMapping("/people/{id}/addresses")
 	 * class AddressController {
@@ -188,7 +183,6 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
 	 * controller.getAddressesForCountry("US")
 	 * builder = MvcUriComponentsBuilder.fromMethodCall(controller);
 	 * 
- * * @param invocationInfo either the value returned from a "mock" controller * invocation or the "mock" controller itself after an invocation * @return a UriComponents instance @@ -206,27 +200,19 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * HandlerMethodMappingNamingStrategy} assigns a default name to every * {@code @RequestMapping} method but an explicit name may also be assigned * through the {@code @RequestMapping} name attribute. - * *

This is intended for use in EL expressions, typically in JSPs or other - * view templates, which can use the convenience method: - * {@link org.springframework.web.servlet.support.RequestContext#getMvcUrl(String, Object...) - * RequestContext.getMvcUrl(String, Object...)}). - * + * view templates, which can use the convenience method {@link #toUriString()}. *

The default naming convention for mappings is based on the capital * letters of the class name, followed by "#" as a separator, and the method * name. For example "TC#getFoo" for a class named TestController with method * getFoo. Use explicit names where the naming convention does not produce * unique results. - * * @param name the mapping name * @param argumentValues argument values for the controller method; those values - * are important for {@code @RequestParam} and {@code @PathVariable} arguments - * but may be passed as {@code null} otherwise. - * + * are important for {@code @RequestParam} and {@code @PathVariable} arguments + * but may be passed as {@code null} otherwise. * @return the UriComponentsBuilder - * - * @throws java.lang.IllegalStateException if the mapping name is not found - * or there is no unique match + * @throws IllegalStateException if the mapping name is not found or there is no unique match */ public static UriComponentsBuilder fromMappingName(String name, Object... argumentValues) { RequestMappingInfoHandlerMapping hm = getRequestMappingInfoHandlerMapping(); @@ -243,20 +229,17 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * {@code @PathVariable} are used for building the URI (via implementations of * {@link org.springframework.web.method.support.UriComponentsContributor}) * while remaining argument values are ignored and can be {@code null}. - * * @param method the controller method * @param argumentValues argument values for the controller method * @return a UriComponentsBuilder instance, never {@code null} */ public static UriComponentsBuilder fromMethod(Method method, Object... argumentValues) { - String typePath = getTypeRequestMapping(method.getDeclaringClass()); String methodPath = getMethodRequestMapping(method); String path = pathMatcher.combine(typePath, methodPath); UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentServletMapping().path(path); UriComponents uriComponents = applyContributors(builder, method, argumentValues); - return ServletUriComponentsBuilder.newInstance().uriComponents(uriComponents); } @@ -287,14 +270,13 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { " does not match number of argument values " + argCount); final Map uriVars = new HashMap(); - for (int i=0; i < paramCount; i++) { + for (int i = 0; i < paramCount; i++) { MethodParameter param = new MethodParameter(method, i); param.initParameterNameDiscovery(parameterNameDiscoverer); contributor.contributeMethodArgument(param, args[i], builder, uriVars); } // We may not have all URI var values, expand only what we have - return builder.build().expand(new UriComponents.UriTemplateVariables() { @Override public Object getValue(String name) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java index 4b30f3023a..276907d8da 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContext.java @@ -47,7 +47,6 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.LocaleContextResolver; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ThemeResolver; -import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; import org.springframework.web.util.HtmlUtils; import org.springframework.web.util.UriTemplate; import org.springframework.web.util.UrlPathHelper; @@ -597,18 +596,6 @@ public class RequestContext { return this.urlPathHelper.getOriginatingQueryString(this.request); } - /** - * Return a URL derived from a controller method's {@code @RequestMapping}. - * This method internally uses - * {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder#fromMappingName(String, Object...) - * MvcUriComponentsBuilder#fromMappingName(String, Object...)}. See its - * Javadoc for more details. - */ - public String getMvcUrl(String mappingName, Object... handlerMethodArguments) { - return MvcUriComponentsBuilder.fromMappingName( - mappingName, handlerMethodArguments).build().encode().toUriString(); - } - /** * Retrieve the message for the given code, using the "defaultHtmlEscape" setting. * @param code code of the message