Class ClassTemplateLoader

  • All Implemented Interfaces:
    TemplateLoader

    public class ClassTemplateLoader
    extends URLTemplateLoader
    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 Detail

      • ClassTemplateLoader

        @Deprecated
        public ClassTemplateLoader()
        Deprecated.
        It's a confusing constructor, and seldom useful; use ClassTemplateLoader(Class, String) instead.
        Creates a template loader that will use the Class.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
        public ClassTemplateLoader​(java.lang.Class<?> resourceLoaderClass)
        Deprecated.
        It's confusing that the base path is ""; use ClassTemplateLoader(Class, String) instead.
        Creates a template loader that will use the Class.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 whose Class.getResource(String) will be used to load the templates.
      • ClassTemplateLoader

        public ClassTemplateLoader​(java.lang.Class<?> resourceLoaderClass,
                                   java.lang.String basePackagePath)
        Creates a template loader that will use the Class.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 whose Class.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 be null.
        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 the resourceLoaderClass 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 be null.
        See Also:
        ClassTemplateLoader(ClassLoader, String)
      • ClassTemplateLoader

        public ClassTemplateLoader​(java.lang.ClassLoader classLoader,
                                   java.lang.String basePackagePath)
        Similar to ClassTemplateLoader(Class, String), but instead of Class.getResource(String) it uses ClassLoader.getResource(String). Because a ClassLoader isn't bound to any Java package, it doesn't mater if the basePackagePath starts with / or not, it will be always relative to the root of the package hierarchy
        Since:
        2.3.22
    • Method Detail

      • getURL

        protected java.net.URL getURL​(java.lang.String name)
        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 class URLTemplateLoader
        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

        public java.lang.String toString()
        Show class name and some details that are useful in template-not-found errors.
        Overrides:
        toString in class java.lang.Object
        Since:
        2.3.21
      • getResourceLoaderClass

        public java.lang.Class getResourceLoaderClass()
        See the similar parameter of ClassTemplateLoader(Class, String); null when other mechanism is used to load the resources.
        Since:
        2.3.22
      • getClassLoader

        public java.lang.ClassLoader getClassLoader()
        See the similar parameter of ClassTemplateLoader(ClassLoader, String); null when other mechanism is used to load the resources.
        Since:
        2.3.22
      • getBasePackagePath

        public java.lang.String getBasePackagePath()
        See the similar parameter of ClassTemplateLoader(ClassLoader, String); note that this is a normalized version of what was actually passed to the constructor.
        Since:
        2.3.22