类 MvcUriComponentsBuilder
UriComponentsBuilder
by pointing to @RequestMapping methods on MVC controllers.
There are several groups of methods:
- Static
fromXxx(...)methods to prepare links using information from the current request as determined by a call toServletUriComponentsBuilder.fromCurrentServletMapping(). - Static
fromXxx(UriComponentsBuilder,...)methods can be given a baseUrl when operating outside the context of a request. - Instance-based
withXxx(...)methods where an instance of MvcUriComponentsBuilder is created with a baseUrl viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).
Note: This class uses values from "Forwarded"
(RFC 7239),
"X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
if present, in order to reflect the client-originated protocol and address.
Consider using the ForwardedHeaderFilter in order to choose from a
central place whether to extract and use, or to discard such headers.
See the Framework reference for more on this filter.
- 从以下版本开始:
- 4.0 2022/4/1 21:59
- 作者:
- Oliver Gierke, Rossen Stoyanchev, Sam Brannen, Harry Yang
-
嵌套类概要
嵌套类修饰符和类型类说明private static classstatic classBuilder class to create URLs for method arguments.static interfaceMethod invocation information. -
字段概要
字段修饰符和类型字段说明private final UriComponentsBuilderprivate static final CompositeUriComponentsContributorprivate static final cn.taketoday.logging.Loggerstatic final StringWell-known name for theCompositeUriComponentsContributorobject in the bean factory.private static final cn.taketoday.core.ParameterNameDiscovererprivate static final cn.taketoday.core.PathMatcher -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private static UriComponentsBuilderapplyContributors(UriComponentsBuilder builder, Method method, Object... args) static <T> Tcontroller(Class<T> controllerType) Return a "mock" controller instance.static UriComponentsBuilderfromController(UriComponentsBuilder builder, Class<?> controllerType) An alternative tofromController(Class)that accepts aUriComponentsBuilderrepresenting the base URL.static UriComponentsBuilderfromController(Class<?> controllerType) Create aUriComponentsBuilderfrom the mapping of a controller class and current request information including Servlet mapping.fromMappingName(UriComponentsBuilder builder, String name) An alternative tofromMappingName(String)that accepts aUriComponentsBuilderrepresenting the base URL.fromMappingName(String mappingName) Create a URL from the name of a MVC controller method's request mapping.static UriComponentsBuilderfromMethod(UriComponentsBuilder baseUrl, Class<?> controllerType, Method method, Object... args) An alternative tofromMethod(Class, Method, Object...)that accepts aUriComponentsBuilderrepresenting the base URL.static UriComponentsBuilderfromMethod(Class<?> controllerType, Method method, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values.static UriComponentsBuilderfromMethodCall(UriComponentsBuilder builder, Object info) An alternative tofromMethodCall(Object)that accepts aUriComponentsBuilderrepresenting the base URL.static UriComponentsBuilderfromMethodCall(Object info) Create aUriComponentsBuilderby invoking a "mock" controller method.private static UriComponentsBuilderfromMethodInternal(UriComponentsBuilder builder, Class<?> controllerType, Method method, Object... args) static UriComponentsBuilderfromMethodName(UriComponentsBuilder builder, Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)that accepts aUriComponentsBuilderrepresenting the base URL.static UriComponentsBuilderfromMethodName(Class<?> controllerType, String methodName, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values.private static cn.taketoday.context.ApplicationContextprivate static UriComponentsBuildergetBaseUrlToUse(UriComponentsBuilder baseUrl) private static StringgetClassMapping(Class<?> controllerType) private static Methodprivate static StringgetMethodMapping(Method method) private static StringgetPathPrefix(Class<?> controllerType) private static CompositeUriComponentsContributorstatic <T> TReturn a "mock" controller instance.static MvcUriComponentsBuilderrelativeTo(UriComponentsBuilder baseUrl) Create an instance of this class with a base URL.withController(Class<?> controllerType) An alternative tofromController(Class)for use with an instance of this class created via a call torelativeTo(cn.taketoday.web.util.UriComponentsBuilder).withMappingName(String mappingName) An alternative tofromMappingName(String)for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).withMethod(Class<?> controllerType, Method method, Object... args) An alternative tofromMethod(Class, Method, Object...)for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).withMethodCall(Object invocationInfo) An alternative tofromMethodCall(Object)for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).withMethodName(Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)} for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).
-
字段详细资料
-
MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME
Well-known name for theCompositeUriComponentsContributorobject in the bean factory.- 另请参阅:
-
logger
private static final cn.taketoday.logging.Logger logger -
pathMatcher
private static final cn.taketoday.core.PathMatcher pathMatcher -
parameterNameDiscoverer
private static final cn.taketoday.core.ParameterNameDiscoverer parameterNameDiscoverer -
defaultUriComponentsContributor
-
baseUrl
-
-
构造器详细资料
-
MvcUriComponentsBuilder
Default constructor. Protected to prevent direct instantiation.
-
-
方法详细资料
-
relativeTo
Create an instance of this class with a base URL. After that calls to one of the instance basedwithXxx(...} methods will create URLs relative to the given base URL. -
fromController
Create aUriComponentsBuilderfrom 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.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
controllerType- the controller to build a URI for- 返回:
- a UriComponentsBuilder instance (never
null)
-
fromController
public static UriComponentsBuilder fromController(@Nullable UriComponentsBuilder builder, Class<?> controllerType) An alternative tofromController(Class)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.controllerType- the controller to build a URI for- 返回:
- a UriComponentsBuilder instance (never
null)
-
fromMethodName
public static UriComponentsBuilder fromMethodName(Class<?> controllerType, String methodName, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values. This method delegates tofromMethod(Class, Method, Object...).Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
controllerType- the controllermethodName- the method nameargs- the argument values- 返回:
- a UriComponentsBuilder instance, never
null - 抛出:
IllegalArgumentException- if there is no matching or if there is more than one matching method
-
fromMethodName
public static UriComponentsBuilder fromMethodName(UriComponentsBuilder builder, Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.controllerType- the controllermethodName- the method nameargs- the argument values- 返回:
- a UriComponentsBuilder instance, never
null - 抛出:
IllegalArgumentException- if there is no matching or if there is more than one matching method
-
fromMethod
public static UriComponentsBuilder fromMethod(Class<?> controllerType, Method method, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values. The array of values must match the signature of the controller method. Values for@RequestParamand@PathVariableare used for building the URI (via implementations ofUriComponentsContributor) while remaining argument values are ignored and can benull.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
controllerType- the controller typemethod- the controller methodargs- argument values for the controller method- 返回:
- a UriComponentsBuilder instance, never
null
-
fromMethod
public static UriComponentsBuilder fromMethod(UriComponentsBuilder baseUrl, @Nullable Class<?> controllerType, Method method, Object... args) An alternative tofromMethod(Class, Method, Object...)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
baseUrl- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.controllerType- the controller typemethod- the controller methodargs- argument values for the controller method- 返回:
- a UriComponentsBuilder instance (never
null)
-
fromMethodCall
Create aUriComponentsBuilderby invoking a "mock" controller method. The controller method and the supplied argument values are then used to delegate tofromMethod(Class, Method, Object...).For example, given this controller:
@RequestMapping("/people/{id}/addresses") class AddressController { @RequestMapping("/{country}") public HttpEntity<Void> getAddressesForCountry(@PathVariable String country) { ... } @RequestMapping(value="/", method=HttpMethod.POST) public void addAddress(Address address) { ... } }A UriComponentsBuilder can be created:// Inline style with static import of "MvcUriComponentsBuilder.on" MvcUriComponentsBuilder.fromMethodCall( on(AddressController.class).getAddressesForCountry("US")).buildAndExpand(1); // Longer form useful for repeated invocation (and void controller methods) AddressController controller = MvcUriComponentsBuilder.on(AddressController.class); controller.addAddress(null); builder = MvcUriComponentsBuilder.fromMethodCall(controller); controller.getAddressesForCountry("US") builder = MvcUriComponentsBuilder.fromMethodCall(controller);Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
info- either the value returned from a "mock" controller invocation or the "mock" controller itself after an invocation- 返回:
- a UriComponents instance
- 另请参阅:
-
fromMethodCall
An alternative tofromMethodCall(Object)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.info- either the value returned from a "mock" controller invocation or the "mock" controller itself after an invocation- 返回:
- a UriComponents instance
-
on
Return a "mock" controller instance. When an@RequestMappingmethod on the controller is invoked, the supplied argument values are remembered and the result can then be used to create aUriComponentsBuilderviafromMethodCall(Object).Note that this is a shorthand version of
controller(Class)intended for inline use (with a static import), for example:MvcUriComponentsBuilder.fromMethodCall(on(FooController.class).getFoo(1)).build();
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
controllerType- the target controller
-
controller
Return a "mock" controller instance. When an@RequestMappingmethod on the controller is invoked, the supplied argument values are remembered and the result can then be used to createUriComponentsBuilderviafromMethodCall(Object).This is a longer version of
on(Class). It is needed with controller methods returning void as well for repeated invocations.FooController fooController = controller(FooController.class); fooController.saveFoo(1, null); builder = MvcUriComponentsBuilder.fromMethodCall(fooController); fooController.saveFoo(2, null); builder = MvcUriComponentsBuilder.fromMethodCall(fooController);
Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
controllerType- the target controller
-
fromMappingName
Create a URL from the name of a MVC controller method's request mapping.The configured
HandlerMethodMappingNamingStrategydetermines the names of controller method request mappings at startup. By default all mappings are assigned a name based on the capital letters of the class name, followed by "#" as separator, and then the method name. For example "PC#getPerson" for a class named PersonController with method getPerson. In case the naming convention does not produce unique results, an explicit name may be assigned through the name attribute of the@RequestMappingannotation.This is aimed primarily for use in view rendering technologies and EL expressions. The URL tag library registers this method as a function called "mvcUrl".
For example, given this controller:
@RequestMapping("/people") class PersonController { @RequestMapping("/{id}") public HttpEntity<Void> getPerson(@PathVariable String id) { ... } }A JSP can prepare a URL to the controller method as follows:<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> <a href="${s:mvcUrl('PC#getPerson').arg(0,"123").build()}">Get Person</a>Note that it's not necessary to specify all arguments. Only the ones required to prepare the URL, mainly
@RequestParamand@PathVariable).Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
mappingName- the mapping name- 返回:
- a builder to prepare the URI String
- 抛出:
IllegalArgumentException- if the mapping name is not found or if there is no unique match
-
fromMappingName
public static MvcUriComponentsBuilder.MethodArgumentBuilder fromMappingName(@Nullable UriComponentsBuilder builder, String name) An alternative tofromMappingName(String)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
- 参数:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.name- the mapping name- 返回:
- a builder to prepare the URI String
- 抛出:
IllegalArgumentException- if the mapping name is not found or if there is no unique match
-
withController
An alternative tofromController(Class)for use with an instance of this class created via a call torelativeTo(cn.taketoday.web.util.UriComponentsBuilder).Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
-
withMethodName
public UriComponentsBuilder withMethodName(Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)} for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
-
withMethodCall
An alternative tofromMethodCall(Object)for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
-
withMappingName
An alternative tofromMappingName(String)for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
-
withMethod
An alternative tofromMethod(Class, Method, Object...)for use with an instance of this class created viarelativeTo(cn.taketoday.web.util.UriComponentsBuilder).Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
-
fromMethodInternal
private static UriComponentsBuilder fromMethodInternal(@Nullable UriComponentsBuilder builder, Class<?> controllerType, Method method, Object... args) -
getBaseUrlToUse
-
getPathPrefix
-
getClassMapping
-
getMethodMapping
-
getMethod
-
applyContributors
private static UriComponentsBuilder applyContributors(UriComponentsBuilder builder, Method method, Object... args) -
getUriComponentsContributor
-
getApplicationContext
@Nullable private static cn.taketoday.context.ApplicationContext getApplicationContext()
-