org.apache.struts2.dispatcher

## Class DefaultStaticContentLoader

- java.lang.Object
- - org.apache.struts2.dispatcher.DefaultStaticContentLoader

- All Implemented Interfaces:
    StaticContentLoader

      public class DefaultStaticContentLoader
      extends Object
      implements 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 /struts/* (or /static/*), and then mapping the value after "/struts/" 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 /struts/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.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected boolean` | `devMode`
    `protected String` | `encoding` Store state of StrutsConstants.STRUTS_I18N_ENCODING setting.
    `protected Calendar` | `lastModifiedCal` Provide a formatted date for setting heading information when caching static content.
    `protected List<String>` | `pathPrefixes` Store set of path prefixes to use with static resources.
    `protected boolean` | `serveStatic` Store state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.
    `protected boolean` | `serveStaticBrowserCache` Store state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultStaticContentLoader()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected String` | `buildPath(String name, String packagePrefix)`
    `boolean` | `canHandle(String resourcePath)`
    `protected String` | `cleanupPath(String path)`
    `protected void` | `copy(InputStream input, OutputStream output)` Copy bytes from the input stream to the output stream.
    `protected URL` | `findResource(String path)` Look for a static resource in the classpath.
    `void` | `findStaticResource(String path, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)` Locate a static resource and copy directly to the response, setting the appropriate caching headers.
    `protected String` | `getAdditionalPackages()`
    `protected String` | `getContentType(String name)` Determine the content type for the resource name.
    `protected List<String>` | `parse(String packages)` Create a string array from a comma-delimited list of packages.
    `protected void` | `process(InputStream is, String path, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)`
    `void` | `setDevMode(String devMode)`
    `void` | `setEncoding(String encoding)` Modify state of StrutsConstants.STRUTS_I18N_ENCODING setting.
    `void` | `setHostConfig(HostConfig filterConfig)`
    `void` | `setServeStaticBrowserCache(String serveStaticBrowserCache)` Modify state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.
    `void` | `setServeStaticContent(String serveStaticContent)` Modify state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### pathPrefixes

          protected List<String> pathPrefixes

      Store set of path prefixes to use with static resources.

    - #### serveStatic

          protected boolean serveStatic

      Store state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.

    - #### serveStaticBrowserCache

          protected boolean serveStaticBrowserCache

      Store state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.

    - #### lastModifiedCal

          protected final Calendar lastModifiedCal

      Provide a formatted date for setting heading information when caching static content.

    - #### encoding

          protected String encoding

      Store state of StrutsConstants.STRUTS_I18N_ENCODING setting.

    - #### devMode

          protected boolean devMode

  - ### Constructor Detail

    - #### DefaultStaticContentLoader

          public DefaultStaticContentLoader()

  - ### Method Detail

    - #### setServeStaticContent

          public void setServeStaticContent(String serveStaticContent)

      Modify state of StrutsConstants.STRUTS_SERVE_STATIC_CONTENT setting.
      Parameters:
        `serveStaticContent` - New setting

    - #### setServeStaticBrowserCache

          public void setServeStaticBrowserCache(String serveStaticBrowserCache)

      Modify state of StrutsConstants.STRUTS_SERVE_STATIC_BROWSER_CACHE setting.
      Parameters:
        `serveStaticBrowserCache` - New setting

    - #### setEncoding

          public void setEncoding(String encoding)

      Modify state of StrutsConstants.STRUTS_I18N_ENCODING setting.
      Parameters:
        `encoding` - New setting

    - #### setDevMode

          public void setDevMode(String devMode)

    - #### setHostConfig

          public void setHostConfig(HostConfig filterConfig)

      Specified by:
        `setHostConfig` in interface `StaticContentLoader`
      Parameters:
        `filterConfig` - The filter configuration

    - #### getAdditionalPackages

          protected String getAdditionalPackages()

    - #### parse

          protected List<String> parse(String packages)

      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,
                                javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response)
                                  throws IOException

      Description copied from interface: `StaticContentLoader`
      Locate a static resource and copy directly to the response, setting the appropriate caching headers.
      Specified by:
        `findStaticResource` in interface `StaticContentLoader`
      Parameters:
        `path` - The resource name
        `request` - The request
        `response` - The response
      Throws:
        `IOException` - If anything goes wrong

    - #### process

          protected void process(InputStream is,
                     String path,
                     javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response)
                          throws IOException

      Throws:
        `IOException`

    - #### findResource

          protected URL findResource(String path)
                              throws IOException

      Look for a static resource in the classpath.
      Parameters:
        `path` - The resource path
      Returns:
        The inputstream of the resource
      Throws:
        `IOException` - If there is a problem locating the resource

    - #### buildPath

          protected String buildPath(String name,
                         String packagePrefix)
                              throws UnsupportedEncodingException

      Parameters:
        `name` - resource name
        `packagePrefix` - The package prefix to use to locate the resource
      Returns:
        full path
      Throws:
        `UnsupportedEncodingException` - If there is a encoding problem

    - #### getContentType

          protected String getContentType(String name)

      Determine the content type for the resource name.
      Parameters:
        `name` - The resource name
      Returns:
        The mime type

    - #### copy

          protected void copy(InputStream input,
                  OutputStream output)
                       throws IOException

      Copy bytes from the input stream to the output stream.
      Parameters:
        `input` - The input stream
        `output` - The output stream
      Throws:
        `IOException` - If anything goes wrong

    - #### canHandle

          public boolean canHandle(String resourcePath)

      Specified by:
        `canHandle` in interface `StaticContentLoader`
      Parameters:
        `resourcePath` - Requested resource path
      Returns:
        true if this loader is able to load this type of resource, false otherwise

    - #### cleanupPath

          protected String cleanupPath(String path)

      Parameters:
        `path` - requested path
      Returns:
        path without leading "/struts" or "/static"

Copyright © 2000–2021 Apache Software Foundation. All rights reserved.