java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractCachingViewResolver
org.springframework.web.servlet.view.UrlBasedViewResolver
org.springframework.web.servlet.view.AbstractTemplateViewResolver
com.github.jknack.handlebars.springmvc.HandlebarsViewResolver
All Implemented Interfaces:
com.github.jknack.handlebars.HelperRegistry, org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.core.Ordered, org.springframework.web.context.ServletContextAware, org.springframework.web.servlet.ViewResolver

public class HandlebarsViewResolver extends org.springframework.web.servlet.view.AbstractTemplateViewResolver implements org.springframework.beans.factory.InitializingBean, com.github.jknack.handlebars.HelperRegistry
A Handlebars view resolver.
Since:
0.1
Author:
edgar.espina
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver

    org.springframework.web.servlet.view.AbstractCachingViewResolver.CacheFilter
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default content type.

    Fields inherited from class org.springframework.web.servlet.view.UrlBasedViewResolver

    FORWARD_URL_PREFIX, REDIRECT_URL_PREFIX

    Fields inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver

    DEFAULT_CACHE_LIMIT

    Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

    logger

    Fields inherited from interface com.github.jknack.handlebars.HelperRegistry

    HELPER_MISSING

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new HandlebarsViewResolver.
    HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars)
    Creates a new HandlebarsViewResolver that utilizes the parameter handlebars for the underlying template lifecycle management.
    HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars, Class<? extends HandlebarsView> viewClass)
    Creates a new HandlebarsViewResolver that utilizes the parameter handlebars for the underlying template lifecycle management.
    Creates a new HandlebarsViewResolver.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected org.springframework.web.servlet.view.AbstractUrlBasedView
    buildView(String viewName)
    protected org.springframework.web.servlet.view.AbstractUrlBasedView
    Configure the handlebars view.
    protected com.github.jknack.handlebars.Handlebars
    createHandlebars(com.github.jknack.handlebars.io.TemplateLoader templateLoader)
    Creates a new Handlebars object using the parameter TemplateLoader.
    protected com.github.jknack.handlebars.io.TemplateLoader
    createTemplateLoader(org.springframework.context.ApplicationContext context)
    Creates a new template loader.
    com.github.jknack.handlebars.Decorator
     
    com.github.jknack.handlebars.Handlebars
    A handlebars instance.
    protected List<com.github.jknack.handlebars.ValueResolver>
     
    <C> com.github.jknack.handlebars.Helper<C>
    helper(String name)
     
    Set<Map.Entry<String,com.github.jknack.handlebars.Helper<?>>>
     
    registerDecorator(String name, com.github.jknack.handlebars.Decorator decorator)
     
    registerHelper(String name, com.github.jknack.handlebars.Helper<H> helper)
     
    registerHelperMissing(com.github.jknack.handlebars.Helper<H> helper)
     
     
    registerHelpers(Class<?> helperSource)
    Register all the helper methods for the given helper source.
    registerHelpers(Object helperSource)
    Register all the helper methods for the given helper source.
    registerHelpers(String filename, InputStream source)
     
    registerHelpers(String filename, Reader source)
     
    registerHelpers(String filename, String source)
     
    registerHelpers(URI location)
     
    protected Class<?>
    The required view class.
    void
    setBindI18nToMessageSource(boolean bindI18nToMessageSource)
     
    void
    setCache(boolean cache)
     
     
    void
    setDeletePartialAfterMerge(boolean deletePartialAfterMerge)
    If true, templates will be deleted once applied.
    void
    setFailOnMissingFile(boolean failOnMissingFile)
    True, if the view resolver should fail on missing files.
    void
    setFormatters(com.github.jknack.handlebars.Formatter... formatters)
    Set variable formatters.
    void
    Set the handlebars.js location used it to compile/precompile template to JavaScript.
    void
    setHelpers(Map<String,com.github.jknack.handlebars.Helper<?>> helpers)
    Register all the helpers in the map.
    void
    setHelperSources(List<?> helpers)
    Register all the helpers in the list.
    void
    setRegisterMessageHelper(boolean registerMessageHelper)
    True, if the message helper (based on MessageSource) should be registered.
    void
    setTemplateCache(com.github.jknack.handlebars.cache.TemplateCache templateCache)
     
    void
    setValueResolvers(com.github.jknack.handlebars.ValueResolver... valueResolvers)
    Set the value resolvers.
    Same as setRegisterMessageHelper(boolean) with a false argument.

    Methods inherited from class org.springframework.web.servlet.view.AbstractTemplateViewResolver

    setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpers

    Methods inherited from class org.springframework.web.servlet.view.UrlBasedViewResolver

    applyLifecycleMethods, canHandle, createView, getAttributesMap, getCacheKey, getContentType, getExposeContextBeansAsAttributes, getExposedContextBeanNames, getExposePathVariables, getOrder, getPrefix, getRedirectHosts, getRequestContextAttribute, getSuffix, getViewClass, getViewNames, initApplicationContext, instantiateView, isRedirectContextRelative, isRedirectHttp10Compatible, loadView, setAttributes, setAttributesMap, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setOrder, setPrefix, setRedirectContextRelative, setRedirectHosts, setRedirectHttp10Compatible, setRequestContextAttribute, setSuffix, setViewClass, setViewNames

    Methods inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver

    clearCache, getCacheFilter, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCacheFilter, setCacheLimit, setCacheUnresolved

    Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

    getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext

    Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

    getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • HandlebarsViewResolver

      public HandlebarsViewResolver(Class<? extends HandlebarsView> viewClass)
      Creates a new HandlebarsViewResolver.
      Parameters:
      viewClass - The view's class. Required.
    • HandlebarsViewResolver

      public HandlebarsViewResolver()
      Creates a new HandlebarsViewResolver.
    • HandlebarsViewResolver

      public HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars)
      Creates a new HandlebarsViewResolver that utilizes the parameter handlebars for the underlying template lifecycle management.
      Parameters:
      handlebars - The Handlebars instance used for template lifecycle management. Required.
    • HandlebarsViewResolver

      public HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars, Class<? extends HandlebarsView> viewClass)
      Creates a new HandlebarsViewResolver that utilizes the parameter handlebars for the underlying template lifecycle management.
      Parameters:
      handlebars - The Handlebars instance used for template lifecycle management. Required.
      viewClass - The view's class. Required.
  • Method Details

    • buildView

      protected org.springframework.web.servlet.view.AbstractUrlBasedView buildView(String viewName) throws Exception
      Overrides:
      buildView in class org.springframework.web.servlet.view.AbstractTemplateViewResolver
      Throws:
      Exception
    • configure

      protected org.springframework.web.servlet.view.AbstractUrlBasedView configure(HandlebarsView view) throws IOException
      Configure the handlebars view.
      Parameters:
      view - The handlebars view.
      Returns:
      The configured view.
      Throws:
      IOException - If a resource cannot be loaded.
    • requiredViewClass

      protected Class<?> requiredViewClass()
      The required view class.
      Overrides:
      requiredViewClass in class org.springframework.web.servlet.view.AbstractTemplateViewResolver
      Returns:
      The required view class.
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • createHandlebars

      protected com.github.jknack.handlebars.Handlebars createHandlebars(com.github.jknack.handlebars.io.TemplateLoader templateLoader)
      Creates a new Handlebars object using the parameter TemplateLoader.
      Parameters:
      templateLoader - A template loader.
      Returns:
      A new handlebar's object.
    • createTemplateLoader

      protected com.github.jknack.handlebars.io.TemplateLoader createTemplateLoader(org.springframework.context.ApplicationContext context)
      Creates a new template loader.
      Parameters:
      context - The application's context.
      Returns:
      A new template loader.
    • getHandlebars

      public com.github.jknack.handlebars.Handlebars getHandlebars()
      A handlebars instance.
      Returns:
      A handlebars instance.
    • getValueResolvers

      protected List<com.github.jknack.handlebars.ValueResolver> getValueResolvers()
      Returns:
      The array of resolvers.
    • setValueResolvers

      public void setValueResolvers(com.github.jknack.handlebars.ValueResolver... valueResolvers)
      Set the value resolvers.
      Parameters:
      valueResolvers - The value resolvers. Required.
    • setFormatters

      public void setFormatters(com.github.jknack.handlebars.Formatter... formatters)
      Set variable formatters.
      Parameters:
      formatters - Formatters to add.
    • setHandlebarsJsFile

      public void setHandlebarsJsFile(String location)
      Set the handlebars.js location used it to compile/precompile template to JavaScript.

      Using handlebars.js 2.x:

       Handlebars handlebars = new Handlebars().handlebarsJsFile("handlebars-v2.0.0.js");
       

      Using handlebars.js 1.x:

       Handlebars handlebars = new Handlebars().handlebarsJsFile("handlebars-v4.7.7.js");
       
      Default handlebars.js is handlebars-v4.7.7.js.
      Parameters:
      location - A classpath location of the handlebar.js file.
    • setFailOnMissingFile

      public void setFailOnMissingFile(boolean failOnMissingFile)
      True, if the view resolver should fail on missing files. Default is: true.
      Parameters:
      failOnMissingFile - True, if the view resolver should fail on missing files. Default is: true.
    • setHelpers

      public void setHelpers(Map<String,com.github.jknack.handlebars.Helper<?>> helpers)
      Register all the helpers in the map.
      Parameters:
      helpers - The helpers to be registered. Required.
      See Also:
      • Handlebars.registerHelper(String, Helper)
    • setHelperSources

      public void setHelperSources(List<?> helpers)
      Register all the helpers in the list. Each element of the list must be a helper source.
      Parameters:
      helpers - The helpers to be registered. Required.
      See Also:
      • Handlebars.registerHelpers(Class)
      • Handlebars.registerHelpers(Object)
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(Object helperSource)
      Register all the helper methods for the given helper source.

      A helper method looks like:

       public static? CharSequence methodName(context?, parameter*, options?) {
       }
       
      Where:
      • A method can/can't be static
      • The method's name became the helper's name
      • Context, parameters and options are all optionals
      • If context and options are present they must be the first and last arguments of the method
      Instance and static methods will be registered as helpers.
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Parameters:
      helperSource - The helper source. Required.
      Returns:
      This handlebars object.
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(Class<?> helperSource)
      Register all the helper methods for the given helper source.

      A helper method looks like:

       public static? CharSequence methodName(context?, parameter*, options?) {
       }
       
      Where:
      • A method can/can't be static
      • The method's name became the helper's name
      • Context, parameters and options are all optionals
      • If context and options are present they must be the first and last arguments of the method
      Only static methods will be registered as helpers.
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Parameters:
      helperSource - The helper source. Required.
      Returns:
      This handlebars object.
    • helper

      public <C> com.github.jknack.handlebars.Helper<C> helper(String name)
      Specified by:
      helper in interface com.github.jknack.handlebars.HelperRegistry
    • helpers

      public Set<Map.Entry<String,com.github.jknack.handlebars.Helper<?>>> helpers()
      Specified by:
      helpers in interface com.github.jknack.handlebars.HelperRegistry
    • registerHelper

      public <H> HandlebarsViewResolver registerHelper(String name, com.github.jknack.handlebars.Helper<H> helper)
      Specified by:
      registerHelper in interface com.github.jknack.handlebars.HelperRegistry
    • registerHelperMissing

      public <H> HandlebarsViewResolver registerHelperMissing(com.github.jknack.handlebars.Helper<H> helper)
      Specified by:
      registerHelperMissing in interface com.github.jknack.handlebars.HelperRegistry
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(URI location) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(File input) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(String filename, Reader source) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(String filename, InputStream source) throws Exception
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      Exception
    • registerHelpers

      public HandlebarsViewResolver registerHelpers(String filename, String source) throws IOException
      Specified by:
      registerHelpers in interface com.github.jknack.handlebars.HelperRegistry
      Throws:
      IOException
    • withoutMessageHelper

      public HandlebarsViewResolver withoutMessageHelper()
      Same as setRegisterMessageHelper(boolean) with a false argument. The message helper wont be registered when you call this method.
      Returns:
      This handlebars view resolver.
    • setRegisterMessageHelper

      public void setRegisterMessageHelper(boolean registerMessageHelper)
      True, if the message helper (based on MessageSource) should be registered. Default is: true.
      Parameters:
      registerMessageHelper - True, if the message helper (based on MessageSource) should be registered. Default is: true.
    • setBindI18nToMessageSource

      public void setBindI18nToMessageSource(boolean bindI18nToMessageSource)
      Parameters:
      bindI18nToMessageSource - If true, the i18n helpers will use a MessageSource instead of a plain ResourceBundle. Default is: false.
    • setDeletePartialAfterMerge

      public void setDeletePartialAfterMerge(boolean deletePartialAfterMerge)
      If true, templates will be deleted once applied. Useful, in some advanced template inheritance use cases. Used by {{#block}} helper. Default is: false. At any time you can override the default setup with:
       {{#block "footer" delete-after-merge=true}}
       
      Parameters:
      deletePartialAfterMerge - True for clearing up templates once they got applied. Used by {{#block}} helper.
    • setCache

      public void setCache(boolean cache)
      Overrides:
      setCache in class org.springframework.web.servlet.view.AbstractCachingViewResolver
    • setTemplateCache

      public void setTemplateCache(com.github.jknack.handlebars.cache.TemplateCache templateCache)
      Parameters:
      templateCache - Set a template cache. Default is: HighConcurrencyTemplateCache.
    • decorator

      public com.github.jknack.handlebars.Decorator decorator(String name)
      Specified by:
      decorator in interface com.github.jknack.handlebars.HelperRegistry
    • registerDecorator

      public HandlebarsViewResolver registerDecorator(String name, com.github.jknack.handlebars.Decorator decorator)
      Specified by:
      registerDecorator in interface com.github.jknack.handlebars.HelperRegistry
    • setCharset

      public HandlebarsViewResolver setCharset(Charset charset)
      Specified by:
      setCharset in interface com.github.jknack.handlebars.HelperRegistry