spring-framework / org.springframework.web.servlet.resource

Package org.springframework.web.servlet.resource

Types

AbstractVersionStrategy

abstract class AbstractVersionStrategy : VersionStrategy

Abstract base class for VersionStrategy implementations.

Supports versions as:

  • prefix in the request path, like "version/static/myresource.js"
  • file name suffix in the request path, like "static/myresource-version.js"

Note: This base class does not provide support for generating the version string.

AppCacheManifestTransformer

open class AppCacheManifestTransformer : ResourceTransformerSupport

A ResourceTransformer implementation that helps handling resources within HTML5 AppCache manifests for HTML5 offline applications.

This transformer:

  • modifies links to match the public URL paths that should be exposed to clients, using configured ResourceResolver strategies
  • appends a comment in the manifest, containing a Hash (e.g. "# Hash: 9de0f09ed7caf84e885f1f0f11c7e326"), thus changing the content of the manifest in order to trigger an appcache reload in the browser.

All files that have the ".appcache" file extension, or the extension given in the constructor, will be transformed by this class. This hash is computed using the content of the appcache manifest and the content of the linked resources; so changing a resource linked in the manifest or the manifest itself should invalidate the browser cache.

In order to serve manifest files with the proper "text/manifest" content type, it is required to configure it with contentNegotiationConfigurer.mediaType("appcache", MediaType.valueOf("text/manifest") in a WebMvcConfigurer.

CachingResourceResolver

open class CachingResourceResolver : AbstractResourceResolver

A org.springframework.web.servlet.resource.ResourceResolver that resolves resources from a org.springframework.cache.Cache or otherwise delegates to the resolver chain and saves the result in the cache.

CachingResourceTransformer

open class CachingResourceTransformer : ResourceTransformer

A org.springframework.web.servlet.resource.ResourceTransformer that checks a org.springframework.cache.Cache to see if a previously transformed resource exists in the cache and returns it if found, and otherwise delegates to the resolver chain and saves the result in the cache.

ContentVersionStrategy

open class ContentVersionStrategy : AbstractVersionStrategy

A VersionStrategy that calculates an Hex MD5 hashes from the content of the resource and appends it to the file name, e.g. "styles/main-e36d2e05253c6c7085a91522ce43a0b4.css".

CssLinkResourceTransformer

open class CssLinkResourceTransformer : ResourceTransformerSupport

A ResourceTransformer implementation that modifies links in a CSS file to match the public URL paths that should be exposed to clients (e.g. with an MD5 content-based hash inserted in the URL).

The implementation looks for links in CSS @import statements and also inside CSS url() functions. All links are then passed through the ResourceResolverChain and resolved relative to the location of the containing CSS file. If successfully resolved, the link is modified, otherwise the original link is preserved.

DefaultServletHttpRequestHandler

open class DefaultServletHttpRequestHandler : HttpRequestHandler, ServletContextAware

An HttpRequestHandler for serving static files using the Servlet container's "default" Servlet.

This handler is intended to be used with a "/*" mapping when the org.springframework.web.servlet.DispatcherServlet is mapped to "/", thus overriding the Servlet container's default handling of static resources. The mapping to this handler should generally be ordered as the last in the chain so that it will only execute when no other more specific mappings (i.e., to controllers) can be matched.

Requests are handled by forwarding through the RequestDispatcher obtained via the name specified through the "defaultServletName" property. In most cases, the defaultServletName does not need to be set explicitly, as the handler checks at initialization time for the presence of the default Servlet of well-known containers such as Tomcat, Jetty, Resin, WebLogic and WebSphere. However, when running in a container where the default Servlet's name is not known, or where it has been customized via server configuration, the defaultServletName will need to be set explicitly.

FixedVersionStrategy

open class FixedVersionStrategy : AbstractVersionStrategy

A VersionStrategy that relies on a fixed version applied as a request path prefix, e.g. reduced SHA, version name, release date, etc.

This is useful for example when ContentVersionStrategy cannot be used such as when using JavaScript module loaders which are in charge of loading the JavaScript resources and need to know their relative paths.

GzipResourceResolver

open class GzipResourceResolver : AbstractResourceResolver

A ResourceResolver that delegates to the chain to locate a resource and then attempts to find a variation with the ".gz" extension.

The resolver gets involved only if the "Accept-Encoding" request header contains the value "gzip" indicating the client accepts gzipped responses.

HttpResource

interface HttpResource : Resource

Extended interface for a Resource to be written to an HTTP response.

PathResourceResolver

open class PathResourceResolver : AbstractResourceResolver

A simple ResourceResolver that tries to find a resource under the given locations matching to the request path.

This resolver does not delegate to the ResourceResolverChain and is expected to be configured at the end in a chain of resolvers.

ResourceResolverChain

interface ResourceResolverChain

A contract for invoking a chain of ResourceResolvers where each resolver is given a reference to the chain allowing it to delegate when necessary.

ResourceTransformer

interface ResourceTransformer

An abstraction for transforming the content of a resource.

ResourceUrlEncodingFilter

open class ResourceUrlEncodingFilter : GenericFilterBean

A filter that wraps the HttpServletResponse and overrides its encodeURL method in order to translate internal resource request URLs into public URL paths for external use.

ResourceUrlProviderExposingInterceptor

open class ResourceUrlProviderExposingInterceptor : HandlerInterceptorAdapter

An interceptor that exposes the ResourceUrlProvider instance it is configured with as a request attribute.

TransformedResource

open class TransformedResource : ByteArrayResource

An extension of ByteArrayResource that a ResourceTransformer can use to represent an original resource preserving all other information except the content.

VersionResourceResolver

open class VersionResourceResolver : AbstractResourceResolver

Resolves request paths containing a version string that can be used as part of an HTTP caching strategy in which a resource is cached with a date in the distant future (e.g. 1 year) and cached until the version, and therefore the URL, is changed.

Different versioning strategies exist, and this resolver must be configured with one or more such strategies along with path mappings to indicate which strategy applies to which resources.

ContentVersionStrategy is a good default choice except in cases where it cannot be used. Most notably the ContentVersionStrategy cannot be combined with JavaScript module loaders. For such cases the FixedVersionStrategy is a better choice.

Note that using this resolver to serve CSS files means that the CssLinkResourceTransformer should also be used in order to modify links within CSS files to also contain the appropriate versions generated by this resolver.

WebJarsResourceResolver

open class WebJarsResourceResolver : AbstractResourceResolver

A ResourceResolver that delegates to the chain to locate a resource and then attempts to find a matching versioned resource contained in a WebJar JAR file.

This allows WebJars.org users to write version agnostic paths in their templates, like <script src="/jquery/jquery.min.js"/>. This path will be resolved to the unique version <script src="/jquery/1.2.0/jquery.min.js"/>, which is a better fit for HTTP caching and version management in applications.

This also resolves resources for version agnostic HTTP requests "GET /jquery/jquery.min.js".

This resolver requires the "org.webjars:webjars-locator" library on classpath, and is automatically registered if that library is present.