Class AbstractViewsRenderer<T,R>

java.lang.Object
io.micronaut.views.AbstractViewsRenderer<T,R>
Type Parameters:
T - The model type
R - The request type
All Implemented Interfaces:
io.micronaut.core.order.Ordered, ViewsRenderer<T,R>
Direct Known Subclasses:
FreemarkerViewsRenderer, HandlebarsViewsRenderer, JinjavaViewsRenderer, RockerViewsRenderer, VelocityViewsRenderer

@Internal public abstract class AbstractViewsRenderer<T,R> extends Object implements ViewsRenderer<T,R>
Base class for view renderers backed by a ViewsRendererConfiguration.

Provides the common template-name normalization and resource-location construction used by renderer implementations. The configured default extension is required when resolving a view. A resource loader is optional so renderers that cannot check for template resources can still use the name and location helpers.

  • Constructor Details

    • AbstractViewsRenderer

      protected AbstractViewsRenderer(@NonNull ViewsRendererConfiguration configuration, @NonNull ViewsConfiguration viewsConfiguration, @NonNull io.micronaut.core.io.ResourceLoader resourceLoader)
      Creates a renderer using the configured views folder and resource loader.
      Parameters:
      configuration - The renderer configuration, including the default template extension
      viewsConfiguration - The shared views configuration
      resourceLoader - The resource loader used to locate templates
    • AbstractViewsRenderer

      protected AbstractViewsRenderer(@NonNull ViewsRendererConfiguration configuration, @NonNull String folder)
      Creates a renderer that only uses the shared name and location helpers.
      Parameters:
      configuration - The renderer configuration, including the default template extension
      folder - The template folder prefix
    • AbstractViewsRenderer

      protected AbstractViewsRenderer(@NonNull ViewsRendererConfiguration configuration, @NonNull String folder, @Nullable io.micronaut.core.io.ResourceLoader resourceLoader)
      Creates a renderer using the supplied template folder and optional resource loader.
      Parameters:
      configuration - The renderer configuration, including the default template extension
      folder - The template folder prefix
      resourceLoader - The resource loader used to locate templates, if supported by the renderer
  • Method Details

    • defaultExtension

      protected final @NonNull String defaultExtension()
      Obtains the configured default template extension.
      Returns:
      The configured default extension
      Throws:
      NullPointerException - If no default extension is configured
    • getTemplate

      protected @NonNull String getTemplate(@NonNull String viewName, @NonNull Charset charset)
      Loads the template identified by the view name from the configured resource loader.
      Parameters:
      viewName - The requested view name, with or without the configured extension
      charset - The character set used to decode the template
      Returns:
      The template text
      Throws:
      ViewRenderingException - If the template cannot be loaded
      NullPointerException - If this renderer has no resource loader
    • viewNameWithExtension

      protected final @NonNull String viewNameWithExtension(@NonNull String name)
      Normalizes a view name and appends the configured default extension.

      The supplied name may already include the configured extension. The configured extension may include a leading .; the returned name always contains exactly one separator.

      Parameters:
      name - The requested view name
      Returns:
      The template name
    • viewLocationWithoutExtension

      protected final @NonNull String viewLocationWithoutExtension(@NonNull String name)
      Resolves a view name relative to the configured template folder without its extension.
      Parameters:
      name - The requested view name
      Returns:
      The folder-qualified template location without its extension
    • viewLocationWithExtension

      protected final @NonNull String viewLocationWithExtension(@NonNull String name)
      Resolves a view name relative to the configured template folder with its default extension.
      Parameters:
      name - The requested view name
      Returns:
      The folder-qualified template location with its extension
    • exists

      public boolean exists(@NonNull String viewName)
      Determines whether a template resource for the given view name is available.

      Returns false when this renderer was created without a resource loader.

      Specified by:
      exists in interface ViewsRenderer<T,R>
      Parameters:
      viewName - The requested view name, with or without the configured extension
      Returns:
      Whether the corresponding template resource exists