Package freemarker.cache
Class ClassTemplateLoader
- java.lang.Object
-
- freemarker.cache.URLTemplateLoader
-
- freemarker.cache.ClassTemplateLoader
-
- All Implemented Interfaces:
TemplateLoader
public class ClassTemplateLoader extends URLTemplateLoader
ATemplateLoaderthat can load templates from the "classpath". Naturally, it can load from jar files, or from anywhere where Java can load classes from. Internally, it usesClass.getResource(String)orClassLoader.getResource(String)to load templates.
-
-
Constructor Summary
Constructors Constructor Description ClassTemplateLoader()Deprecated.It's a confusing constructor, and seldom useful; useClassTemplateLoader(Class, String)instead.ClassTemplateLoader(Class resourceLoaderClass)Deprecated.It's confusing that the base path is""; useClassTemplateLoader(Class, String)instead.ClassTemplateLoader(ClassLoader classLoader, String basePackagePath)Similar toClassTemplateLoader(Class, String), but instead ofClass.getResource(String)it usesClassLoader.getResource(String).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).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetBasePackagePath()See the similar parameter ofClassTemplateLoader(ClassLoader, String); note that this is a normalized version of what was actually passed to the constructor.ClassLoadergetClassLoader()See the similar parameter ofClassTemplateLoader(ClassLoader, String);nullwhen other mechanism is used to load the resources.ClassgetResourceLoaderClass()See the similar parameter ofClassTemplateLoader(Class, String);nullwhen other mechanism is used to load the resources.protected URLgetURL(String name)Given a template name (plus potential locale decorations) retrieves an URL that points the template source.StringtoString()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 Detail
-
ClassTemplateLoader
public 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
public ClassTemplateLoader(Class resourceLoaderClass)
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
public 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).Examples:
- Relative base path (will load from the
com.example.myapplication.templatespackage):
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 theresourceLoaderClassclass. 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
public ClassTemplateLoader(ClassLoader classLoader, String basePackagePath)
Similar toClassTemplateLoader(Class, String), but instead ofClass.getResource(String)it usesClassLoader.getResource(String). Because aClassLoaderisn't bound to any Java package, it doesn't mater if thebasePackagePathstarts with/or not, it will be always relative to the root of the package hierarchy- Since:
- 2.3.22
-
-
Method Detail
-
getURL
protected URL getURL(String name)
Description copied from class:URLTemplateLoaderGiven a template name (plus potential locale decorations) retrieves an URL that points the template source.- Specified by:
getURLin 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 it can determine that the template source does not exist.
-
toString
public String toString()
Show class name and some details that are useful in template-not-found errors.
-
getResourceLoaderClass
public Class getResourceLoaderClass()
See the similar parameter ofClassTemplateLoader(Class, String);nullwhen other mechanism is used to load the resources.- Since:
- 2.3.22
-
getClassLoader
public ClassLoader getClassLoader()
See the similar parameter ofClassTemplateLoader(ClassLoader, String);nullwhen other mechanism is used to load the resources.- Since:
- 2.3.22
-
getBasePackagePath
public String 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
-
-