Package freemarker.cache
Class ClassTemplateLoader
java.lang.Object
freemarker.cache.URLTemplateLoader
freemarker.cache.ClassTemplateLoader
- All Implemented Interfaces:
TemplateLoader
A
TemplateLoader
that can load templates from the "classpath". Naturally, it can load from jar files, or from
anywhere where Java can load classes from. Internally, it uses Class.getResource(String)
or
ClassLoader.getResource(String)
to load templates.-
Constructor Summary
ConstructorDescriptionDeprecated.ClassTemplateLoader(Class<?> resourceLoaderClass)
Deprecated.It's confusing that the base path is""
; useClassTemplateLoader(Class, String)
instead.ClassTemplateLoader(Class<?> resourceLoaderClass, String basePackagePath)
Creates a template loader that will use theClass.getResource(String)
method of the specified class to load the resources, and the specified base package path (absolute or relative).ClassTemplateLoader(ClassLoader classLoader, String basePackagePath)
Similar toClassTemplateLoader(Class, String)
, but instead ofClass.getResource(String)
it usesClassLoader.getResource(String)
. -
Method Summary
Modifier and TypeMethodDescriptionSee the similar parameter ofClassTemplateLoader(ClassLoader, String)
; note that this is a normalized version of what was actually passed to the constructor.See the similar parameter ofClassTemplateLoader(ClassLoader, String)
;null
when other mechanism is used to load the resources.See the similar parameter ofClassTemplateLoader(Class, String)
;null
when other mechanism is used to load the resources.protected URL
Given a template name (plus potential locale decorations) retrieves an URL that points the template source.toString()
Show class name and some details that are useful in template-not-found errors.Methods inherited from class freemarker.cache.URLTemplateLoader
canonicalizePrefix, closeTemplateSource, findTemplateSource, getLastModified, getReader, getURLConnectionUsesCaches, setURLConnectionUsesCaches
-
Constructor Details
-
ClassTemplateLoader
Deprecated.It's a confusing constructor, and seldom useful; useClassTemplateLoader(Class, String)
instead.Creates a template loader that will use theClass.getResource(String)
method of its own class to load the resources, and"/"
as base package path. This means that that template paths will be resolved relatively the root package of the class hierarchy, so you hardly ever should use this constructor, rather do something like this:
new ClassTemplateLoader(com.example.myapplication.SomeClass.class, "templates")
If you extend this class, then the extending class will be used to load the resources.
-
ClassTemplateLoader
Deprecated.It's confusing that the base path is""
; useClassTemplateLoader(Class, String)
instead.Creates a template loader that will use theClass.getResource(String)
method of the specified class to load the resources, and""
as base package path. This means that template paths will be resolved relatively to the class location, that is, relatively to the directory (package) of the class.- Parameters:
resourceLoaderClass
- the class whoseClass.getResource(String)
will be used to load the templates.
-
ClassTemplateLoader
Creates a template loader that will use theClass.getResource(String)
method of the specified class to load the resources, and the specified base package path (absolute or relative).Examples:
- Relative base path (will load from the
com.example.myapplication.templates
package):
new ClassTemplateLoader(com.example.myapplication.SomeClass.class, "templates")
- Absolute base path:
new ClassTemplateLoader(somepackage.SomeClass.class, "/com/example/myapplication/templates")
- Parameters:
resourceLoaderClass
- The class whoseClass.getResource(String)
method will be used to load the templates. Be sure that you chose a class whose defining class-loader sees the templates. This parameter can't benull
.basePackagePath
- The package that contains the templates, in path (/
-separated) format. If it doesn't start with a/
then it's relative to the path (package) of theresourceLoaderClass
class. If it starts with/
then it's relative to the root of the package hierarchy. Note that path components should be separated by forward slashes independently of the separator character used by the underlying operating system. This parameter can't benull
.- See Also:
ClassTemplateLoader(ClassLoader, String)
- Relative base path (will load from the
-
ClassTemplateLoader
Similar toClassTemplateLoader(Class, String)
, but instead ofClass.getResource(String)
it usesClassLoader.getResource(String)
. Because aClassLoader
isn't bound to any Java package, it doesn't mater if thebasePackagePath
starts with/
or not, it will be always relative to the root of the package hierarchy- Since:
- 2.3.22
-
-
Method Details
-
getURL
Description copied from class:URLTemplateLoader
Given a template name (plus potential locale decorations) retrieves an URL that points the template source.- Specified by:
getURL
in classURLTemplateLoader
- Parameters:
name
- the name of the sought template, including the locale decorations.- Returns:
- an URL that points to the template source, or
null
if the template does not exist.
-
toString
Show class name and some details that are useful in template-not-found errors. -
getResourceLoaderClass
See the similar parameter ofClassTemplateLoader(Class, String)
;null
when other mechanism is used to load the resources.- Since:
- 2.3.22
-
getClassLoader
See the similar parameter ofClassTemplateLoader(ClassLoader, String)
;null
when other mechanism is used to load the resources.- Since:
- 2.3.22
-
getBasePackagePath
See the similar parameter ofClassTemplateLoader(ClassLoader, String)
; note that this is a normalized version of what was actually passed to the constructor.- Since:
- 2.3.22
-
ClassTemplateLoader(Class, String)
instead.