Class DefaultStaticContentLoader
- All Implemented Interfaces:
StaticContentLoader
Default implementation to server static content
This class is used to serve common static content needed when using various parts of Struts, such as JavaScript
files, CSS files, etc. It works by looking for requests to uiStaticContentPath/* and then mapping the value
after to common packages in Struts and, optionally, in your class path. By default, the following packages are
automatically searched:
- org.apache.struts2.static
- template
- static
This means that you can simply request uiStaticContentPath/xhtml/styles.css and the XHTML UI theme's default stylesheet
will be returned. Likewise, many of the AJAX UI components require various JavaScript files, which are found in the
org.apache.struts2.static package. If you wish to add additional packages to be searched, you can add a comma
separated (space, tab and new line will do as well) list in the filter init parameter named "packages". Be
careful, however, to expose any packages that may have sensitive information, such as properties file with
database access credentials.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.struts2.dispatcher.StaticContentLoader
StaticContentLoader.Validator -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected final CalendarProvide a formatted date for setting heading information when caching static content.Store set of path prefixes to use with static resources.protected booleanStore state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.protected booleanStore state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.protected StringStore state ofStrutsConstants.STRUTS_UI_STATIC_CONTENT_PATHsetting.protected static final Stringprotected WebJarUrlProviderFields inherited from interface org.apache.struts2.dispatcher.StaticContentLoader
DEFAULT_STATIC_CONTENT_PATH -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Stringbooleanprotected StringcleanupPath(String path) protected voidcopy(InputStream input, OutputStream output) Copy bytes from the input stream to the output stream.protected URLfindResource(String path) Look for a static resource in the classpath.voidfindStaticResource(String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Locate a static resource and copy directly to the response, setting the appropriate caching headers.protected booleanfindWebJarResource(String name, String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Resolve and serve a WebJar asset requested under<staticContentPath>/webjars/**.protected Stringprotected StringgetContentType(String name) Determine the content type for the resource name.Create a string array from a comma-delimited list of packages.protected voidprocess(InputStream is, String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) protected voidsendNotFound(jakarta.servlet.http.HttpServletResponse response) voidsetDevMode(String devMode) voidsetHostConfig(HostConfig filterConfig) voidsetServeStaticBrowserCache(String serveStaticBrowserCache) Modify state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.voidsetServeStaticContent(String serveStaticContent) Modify state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.voidsetStaticContentPath(String uiStaticContentPath) voidsetWebJarUrlProvider(WebJarUrlProvider webJarUrlProvider)
-
Field Details
-
WEBJARS_REQUEST_PREFIX
- See Also:
-
pathPrefixes
Store set of path prefixes to use with static resources. -
serveStatic
protected boolean serveStaticStore state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting. -
uiStaticContentPath
Store state ofStrutsConstants.STRUTS_UI_STATIC_CONTENT_PATHsetting. -
serveStaticBrowserCache
protected boolean serveStaticBrowserCacheStore state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting. -
lastModifiedCal
Provide a formatted date for setting heading information when caching static content. -
devMode
protected boolean devMode -
webJarUrlProvider
-
-
Constructor Details
-
DefaultStaticContentLoader
public DefaultStaticContentLoader()
-
-
Method Details
-
setWebJarUrlProvider
-
setServeStaticContent
Modify state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.- Parameters:
serveStaticContent- New setting
-
setStaticContentPath
-
setServeStaticBrowserCache
Modify state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.- Parameters:
serveStaticBrowserCache- New setting
-
setDevMode
-
setHostConfig
- Specified by:
setHostConfigin interfaceStaticContentLoader- Parameters:
filterConfig- The filter configuration
-
getAdditionalPackages
-
parse
Create a string array from a comma-delimited list of packages.- Parameters:
packages- A comma-delimited String listing packages- Returns:
- A string array of packages
-
findStaticResource
public void findStaticResource(String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException Description copied from interface:StaticContentLoaderLocate a static resource and copy directly to the response, setting the appropriate caching headers.- Specified by:
findStaticResourcein interfaceStaticContentLoader- Parameters:
path- The resource namerequest- The requestresponse- The response- Throws:
IOException- If anything goes wrong
-
findWebJarResource
protected boolean findWebJarResource(String name, String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException Resolve and serve a WebJar asset requested under<staticContentPath>/webjars/**.- Parameters:
name- the request path with the static-content prefix stripped, e.g./webjars/jquery/jquery.min.jspath- the original request path (used for content-type detection)- Returns:
- true if the asset was resolved and streamed; false otherwise (caller sends 404)
- Throws:
IOException
-
sendNotFound
protected void sendNotFound(jakarta.servlet.http.HttpServletResponse response) -
process
protected void process(InputStream is, String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException - Throws:
IOException
-
findResource
Look for a static resource in the classpath.- Parameters:
path- The resource path- Returns:
- The URL of the resource
- Throws:
IOException- If there is a problem locating the resource
-
buildPath
- Parameters:
name- resource namepackagePrefix- The package prefix to use to locate the resource- Returns:
- full path
-
getContentType
Determine the content type for the resource name.- Parameters:
name- The resource name- Returns:
- The mime type, or
nullif the extension is unknown
-
copy
Copy bytes from the input stream to the output stream.- Parameters:
input- The input streamoutput- The output stream- Throws:
IOException- If anything goes wrong
-
canHandle
- Specified by:
canHandlein interfaceStaticContentLoader- Parameters:
resourcePath- Requested resource path- Returns:
- true if this loader is able to load this type of resource, false otherwise
-
cleanupPath
- Parameters:
path- requested path- Returns:
- path without leading
uiStaticContentPath
-