Class WebappResourceLoader
java.lang.Object
org.apache.velocity.runtime.resource.loader.ResourceLoader
org.apache.velocity.tools.view.WebappResourceLoader
public class WebappResourceLoader
extends org.apache.velocity.runtime.resource.loader.ResourceLoader
Resource loader that uses the ServletContext of a webapp to
load Velocity templates. (it's much easier to use with servlets than
the standard FileResourceLoader, in particular the use of war files
is transparent).
The default search path is '/' (relative to the webapp root), but
you can change this behaviour by specifying one or more paths
by mean of as many resource.loader.webapp.path properties as needed
in the velocity.properties file.
All paths must be relative to the root of the webapp.
To enable caching and cache refreshing the resource.loader.webapp.cache and
resource.loader.webapp.modificationCheckInterval properties need to be
set in the velocity.properties file ... auto-reloading of global macros
requires the resource.loader.webapp.cache property to be set to 'false'.
- Version:
- $Id$
- Author:
- Geir Magnusson Jr., Nathan Bubna, Claude Brisson
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String[]The root paths for templates (relative to webapp's root).protected javax.servlet.ServletContextprotected HashMapFields inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
className, isCachingOn, log, modificationCheckInterval, rsvc -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlonggetLastModified(org.apache.velocity.runtime.resource.Resource resource) Checks to see when a resource was last modifiedgetResourceReader(String name, String encoding) Get Reader so that the Runtime can build a template with it.voidinit(org.apache.velocity.util.ExtProperties configuration) This is abstract in the base class, so we need it.booleanisSourceModified(org.apache.velocity.runtime.resource.Resource resource) Checks to see if a resource has been deleted, moved or modified.Methods inherited from class org.apache.velocity.runtime.resource.loader.ResourceLoader
buildReader, commonInit, getClassName, getModificationCheckInterval, isCachingOn, resourceExists, setCachingOn, setModificationCheckInterval
-
Field Details
-
paths
The root paths for templates (relative to webapp's root). -
templatePaths
-
servletContext
protected javax.servlet.ServletContext servletContext
-
-
Constructor Details
-
WebappResourceLoader
public WebappResourceLoader()
-
-
Method Details
-
init
public void init(org.apache.velocity.util.ExtProperties configuration) This is abstract in the base class, so we need it.
NOTE: this expects that the ServletContext has already been placed in the runtime's application attributes under its full class name (i.e. "javax.servlet.ServletContext").- Specified by:
initin classorg.apache.velocity.runtime.resource.loader.ResourceLoader- Parameters:
configuration- theExtPropertiesassociated with this resource loader.
-
getResourceReader
public Reader getResourceReader(String name, String encoding) throws org.apache.velocity.exception.ResourceNotFoundException Get Reader so that the Runtime can build a template with it.- Specified by:
getResourceReaderin classorg.apache.velocity.runtime.resource.loader.ResourceLoader- Parameters:
name- name of template to getencoding- asked encoding- Returns:
- InputStream containing the template
- Throws:
org.apache.velocity.exception.ResourceNotFoundException- if template not found in classpath.- Since:
- 2.0
-
isSourceModified
public boolean isSourceModified(org.apache.velocity.runtime.resource.Resource resource) Checks to see if a resource has been deleted, moved or modified.- Specified by:
isSourceModifiedin classorg.apache.velocity.runtime.resource.loader.ResourceLoader- Parameters:
resource- Resource The resource to check for modification- Returns:
- boolean True if the resource has been modified
-
getLastModified
public long getLastModified(org.apache.velocity.runtime.resource.Resource resource) Checks to see when a resource was last modified- Specified by:
getLastModifiedin classorg.apache.velocity.runtime.resource.loader.ResourceLoader- Parameters:
resource- Resource the resource to check- Returns:
- long The time when the resource was last modified or 0 if the file can't be read
-