类 UrlPathHelper
- java.lang.Object
- org.springframework.web.util.UrlPathHelper
public class UrlPathHelper extends Object
Helper class for URL path matching. Provides support for URL paths inRequestDispatcherincludes and support for consistent URL decoding.Used by
AbstractUrlHandlerMappingandRequestContextfor path matching and/or URI determination.- 从以下版本开始:
- 14.01.2004
- 作者:
- Juergen Hoeller, Rob Harrop, Rossen Stoyanchev
- 另请参阅:
getLookupPathForRequest(javax.servlet.http.HttpServletRequest),RequestDispatcher
字段概要
字段 修饰符和类型 字段 说明 static UrlPathHelperdefaultInstanceShared, read-only instance with defaults.static UrlPathHelperrawPathInstanceShared, read-only instance for the full, encoded path.
构造器概要
构造器 构造器 说明 UrlPathHelper()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 MultiValueMap<String,String>decodeMatrixVariables(HttpServletRequest request, MultiValueMap<String,String> vars)Decode the given matrix variables viadecodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).Map<String,String>decodePathVariables(HttpServletRequest request, Map<String,String> vars)Decode the given URI path variables viadecodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).StringdecodeRequestString(HttpServletRequest request, String source)Decode the given source string with a URLDecoder.protected StringdetermineEncoding(HttpServletRequest request)Determine the encoding for the given request.StringgetContextPath(HttpServletRequest request)Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.protected StringgetDefaultEncoding()Return the default character encoding to use for URL decoding.StringgetLookupPathForRequest(HttpServletRequest request)Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.StringgetLookupPathForRequest(HttpServletRequest request, String lookupPathAttributeName)Variant ofgetLookupPathForRequest(HttpServletRequest)that automates checking for a previously computed lookupPath saved as a request attribute.StringgetOriginatingContextPath(HttpServletRequest request)Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.StringgetOriginatingQueryString(HttpServletRequest request)Return the query string part of the given request's URL.StringgetOriginatingRequestUri(HttpServletRequest request)Return the request URI for the given request.StringgetOriginatingServletPath(HttpServletRequest request)Return the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.StringgetPathWithinApplication(HttpServletRequest request)Return the path within the web application for the given request.StringgetPathWithinServletMapping(HttpServletRequest request)Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.protected StringgetPathWithinServletMapping(HttpServletRequest request, String pathWithinApp)Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.StringgetRequestUri(HttpServletRequest request)Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.StringgetServletPath(HttpServletRequest request)Return the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.booleanisUrlDecode()Whether to decode the request URI when determining the lookup path.StringremoveSemicolonContent(String requestUri)Remove ";" (semicolon) content from the given request URI if the removeSemicolonContent property is set to "true".voidsetAlwaysUseFullPath(boolean alwaysUseFullPath)Whether URL lookups should always use the full path within the current web application context, i.e. withinServletContext.getContextPath().voidsetDefaultEncoding(String defaultEncoding)Set the default character encoding to use for URL decoding.voidsetRemoveSemicolonContent(boolean removeSemicolonContent)Set if ";" (semicolon) content should be stripped from the request URI.voidsetUrlDecode(boolean urlDecode)Whether the context path and request URI should be decoded -- both of which are returned undecoded by the Servlet API, in contrast to the servlet path.booleanshouldRemoveSemicolonContent()Whether configured to remove ";" (semicolon) content from the request URI.
字段详细资料
defaultInstance
public static final UrlPathHelper defaultInstance
Shared, read-only instance with defaults. The following apply:alwaysUseFullPath=falseurlDecode=trueremoveSemicolon=truedefaultEncoding=WebUtils.DEFAULT_CHARACTER_ENCODING
rawPathInstance
public static final UrlPathHelper rawPathInstance
Shared, read-only instance for the full, encoded path. The following apply:alwaysUseFullPath=trueurlDecode=falseremoveSemicolon=falsedefaultEncoding=WebUtils.DEFAULT_CHARACTER_ENCODING
构造器详细资料
UrlPathHelper
public UrlPathHelper()
方法详细资料
setAlwaysUseFullPath
public void setAlwaysUseFullPath(boolean alwaysUseFullPath)
Whether URL lookups should always use the full path within the current web application context, i.e. withinServletContext.getContextPath().If set to false the path within the current servlet mapping is used instead if applicable (i.e. in the case of a prefix based Servlet mapping such as "/myServlet/*").
By default this is set to "false".
setUrlDecode
public void setUrlDecode(boolean urlDecode)
Whether the context path and request URI should be decoded -- both of which are returned undecoded by the Servlet API, in contrast to the servlet path.Either the request encoding or the default Servlet spec encoding (ISO-8859-1) is used when set to "true".
By default this is set to true.
Note: Be aware the servlet path will not match when compared to encoded paths. Therefore use of
urlDecode=falseis not compatible with a prefix-based Servlet mapping and likewise implies also settingalwaysUseFullPath=true.
isUrlDecode
public boolean isUrlDecode()
Whether to decode the request URI when determining the lookup path.- 从以下版本开始:
- 4.3.13
setRemoveSemicolonContent
public void setRemoveSemicolonContent(boolean removeSemicolonContent)
Set if ";" (semicolon) content should be stripped from the request URI.Default is "true".
shouldRemoveSemicolonContent
public boolean shouldRemoveSemicolonContent()
Whether configured to remove ";" (semicolon) content from the request URI.
setDefaultEncoding
public void setDefaultEncoding(String defaultEncoding)
Set the default character encoding to use for URL decoding. Default is ISO-8859-1, according to the Servlet spec.If the request specifies a character encoding itself, the request encoding will override this setting. This also allows for generically overriding the character encoding in a filter that invokes the
ServletRequest.setCharacterEncodingmethod.- 参数:
defaultEncoding- the character encoding to use- 另请参阅:
determineEncoding(javax.servlet.http.HttpServletRequest),ServletRequest.getCharacterEncoding(),ServletRequest.setCharacterEncoding(String),WebUtils.DEFAULT_CHARACTER_ENCODING
getDefaultEncoding
protected String getDefaultEncoding()
Return the default character encoding to use for URL decoding.
getLookupPathForRequest
public String getLookupPathForRequest(HttpServletRequest request)
Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.Detects include request URL if called within a RequestDispatcher include.
- 参数:
request- current HTTP request- 返回:
- the lookup path
- 另请参阅:
getPathWithinServletMapping(javax.servlet.http.HttpServletRequest),getPathWithinApplication(javax.servlet.http.HttpServletRequest)
getLookupPathForRequest
public String getLookupPathForRequest(HttpServletRequest request, @Nullable String lookupPathAttributeName)
Variant ofgetLookupPathForRequest(HttpServletRequest)that automates checking for a previously computed lookupPath saved as a request attribute. The attribute is only used for lookup purposes.- 参数:
request- current HTTP requestlookupPathAttributeName- the request attribute to check- 返回:
- the lookup path
- 从以下版本开始:
- 5.2
- 另请参阅:
HandlerMapping.LOOKUP_PATH
getPathWithinServletMapping
public String getPathWithinServletMapping(HttpServletRequest request)
Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.- 参数:
request- current HTTP request- 返回:
- the path within the servlet mapping, or ""
- 另请参阅:
getPathWithinServletMapping(HttpServletRequest, String)
getPathWithinServletMapping
protected String getPathWithinServletMapping(HttpServletRequest request, String pathWithinApp)
Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.Detects include request URL if called within a RequestDispatcher include.
E.g.: servlet mapping="/*"; request URI="/test/a" -> "/test/a".
E.g.: servlet mapping="/"; request URI="/test/a" -> "/test/a".
E.g.: servlet mapping="/test/*"; request URI="/test/a" -> "/a".
E.g.: servlet mapping="/test"; request URI="/test" -> "".
E.g.: servlet mapping="/*.test"; request URI="/a.test" -> "".
- 参数:
request- current HTTP requestpathWithinApp- a precomputed path within the application- 返回:
- the path within the servlet mapping, or ""
- 从以下版本开始:
- 5.2.9
- 另请参阅:
getLookupPathForRequest(javax.servlet.http.HttpServletRequest)
getPathWithinApplication
public String getPathWithinApplication(HttpServletRequest request)
Return the path within the web application for the given request.Detects include request URL if called within a RequestDispatcher include.
- 参数:
request- current HTTP request- 返回:
- the path within the web application
- 另请参阅:
getLookupPathForRequest(javax.servlet.http.HttpServletRequest)
getRequestUri
public String getRequestUri(HttpServletRequest request)
Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.As the value returned by
request.getRequestURI()is not decoded by the servlet container, this method will decode it.The URI that the web container resolves should be correct, but some containers like JBoss/Jetty incorrectly include ";" strings like ";jsessionid" in the URI. This method cuts off such incorrect appendices.
- 参数:
request- current HTTP request- 返回:
- the request URI
getContextPath
public String getContextPath(HttpServletRequest request)
Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.As the value returned by
request.getContextPath()is not decoded by the servlet container, this method will decode it.- 参数:
request- current HTTP request- 返回:
- the context path
getServletPath
public String getServletPath(HttpServletRequest request)
Return the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.As the value returned by
request.getServletPath()is already decoded by the servlet container, this method will not attempt to decode it.- 参数:
request- current HTTP request- 返回:
- the servlet path
getOriginatingRequestUri
public String getOriginatingRequestUri(HttpServletRequest request)
Return the request URI for the given request. If this is a forwarded request, correctly resolves to the request URI of the original request.
getOriginatingContextPath
public String getOriginatingContextPath(HttpServletRequest request)
Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.As the value returned by
request.getContextPath()is not decoded by the servlet container, this method will decode it.- 参数:
request- current HTTP request- 返回:
- the context path
getOriginatingServletPath
public String getOriginatingServletPath(HttpServletRequest request)
Return the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.- 参数:
request- current HTTP request- 返回:
- the servlet path
getOriginatingQueryString
public String getOriginatingQueryString(HttpServletRequest request)
Return the query string part of the given request's URL. If this is a forwarded request, correctly resolves to the query string of the original request.- 参数:
request- current HTTP request- 返回:
- the query string
decodeRequestString
public String decodeRequestString(HttpServletRequest request, String source)
Decode the given source string with a URLDecoder. The encoding will be taken from the request, falling back to the default "ISO-8859-1".The default implementation uses
URLDecoder.decode(input, enc).- 参数:
request- current HTTP requestsource- the String to decode- 返回:
- the decoded String
- 另请参阅:
WebUtils.DEFAULT_CHARACTER_ENCODING,ServletRequest.getCharacterEncoding(),URLDecoder.decode(String, String),URLDecoder.decode(String)
determineEncoding
protected String determineEncoding(HttpServletRequest request)
Determine the encoding for the given request. Can be overridden in subclasses.The default implementation checks the request encoding, falling back to the default encoding specified for this resolver.
- 参数:
request- current HTTP request- 返回:
- the encoding for the request (never
null) - 另请参阅:
ServletRequest.getCharacterEncoding(),setDefaultEncoding(java.lang.String)
removeSemicolonContent
public String removeSemicolonContent(String requestUri)
Remove ";" (semicolon) content from the given request URI if the removeSemicolonContent property is set to "true". Note that "jsessionid" is always removed.- 参数:
requestUri- the request URI string to remove ";" content from- 返回:
- the updated URI string
decodePathVariables
public Map<String,String> decodePathVariables(HttpServletRequest request, Map<String,String> vars)
Decode the given URI path variables viadecodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).- 参数:
request- current HTTP requestvars- the URI variables extracted from the URL path- 返回:
- the same Map or a new Map instance
decodeMatrixVariables
public MultiValueMap<String,String> decodeMatrixVariables(HttpServletRequest request, MultiValueMap<String,String> vars)
Decode the given matrix variables viadecodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String)unlesssetUrlDecode(boolean)is set totruein which case it is assumed the URL path from which the variables were extracted is already decoded through a call togetLookupPathForRequest(HttpServletRequest).- 参数:
request- current HTTP requestvars- the URI variables extracted from the URL path- 返回:
- the same Map or a new Map instance