Class HandlebarsViewResolver
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
FieldsFields 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
ConstructorsConstructorDescriptionCreates a newHandlebarsViewResolver
.HandlebarsViewResolver
(com.github.jknack.handlebars.Handlebars handlebars) Creates a newHandlebarsViewResolver
that utilizes the parameter handlebars for the underlying template lifecycle management.HandlebarsViewResolver
(com.github.jknack.handlebars.Handlebars handlebars, Class<? extends HandlebarsView> viewClass) Creates a newHandlebarsViewResolver
that utilizes the parameter handlebars for the underlying template lifecycle management.HandlebarsViewResolver
(Class<? extends HandlebarsView> viewClass) Creates a newHandlebarsViewResolver
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
protected org.springframework.web.servlet.view.AbstractUrlBasedView
protected org.springframework.web.servlet.view.AbstractUrlBasedView
configure
(HandlebarsView view) Configure the handlebars view.protected com.github.jknack.handlebars.Handlebars
createHandlebars
(com.github.jknack.handlebars.io.TemplateLoader templateLoader) Creates a newHandlebars
object using the parameterTemplateLoader
.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>
helpers()
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
(File input) 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) setCharset
(Charset charset) 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
setHandlebarsJsFile
(String location) 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 onMessageSource
) 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 assetRegisterMessageHelper(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
-
Field Details
-
DEFAULT_CONTENT_TYPE
The default content type.- See Also:
-
-
Constructor Details
-
HandlebarsViewResolver
Creates a newHandlebarsViewResolver
.- Parameters:
viewClass
- The view's class. Required.
-
HandlebarsViewResolver
public HandlebarsViewResolver()Creates a newHandlebarsViewResolver
. -
HandlebarsViewResolver
public HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars) Creates a newHandlebarsViewResolver
that utilizes the parameter handlebars for the underlying template lifecycle management.- Parameters:
handlebars
- TheHandlebars
instance used for template lifecycle management. Required.
-
HandlebarsViewResolver
public HandlebarsViewResolver(com.github.jknack.handlebars.Handlebars handlebars, Class<? extends HandlebarsView> viewClass) Creates a newHandlebarsViewResolver
that utilizes the parameter handlebars for the underlying template lifecycle management.- Parameters:
handlebars
- TheHandlebars
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 classorg.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
The required view class.- Overrides:
requiredViewClass
in classorg.springframework.web.servlet.view.AbstractTemplateViewResolver
- Returns:
- The required view class.
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
createHandlebars
protected com.github.jknack.handlebars.Handlebars createHandlebars(com.github.jknack.handlebars.io.TemplateLoader templateLoader) Creates a newHandlebars
object using the parameterTemplateLoader
.- 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
- 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
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 ishandlebars-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
Register all the helpers in the map.- Parameters:
helpers
- The helpers to be registered. Required.- See Also:
-
Handlebars.registerHelper(String, Helper)
-
setHelperSources
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
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
- Specified by:
registerHelpers
in interfacecom.github.jknack.handlebars.HelperRegistry
- Parameters:
helperSource
- The helper source. Required.- Returns:
- This handlebars object.
-
registerHelpers
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
- Specified by:
registerHelpers
in interfacecom.github.jknack.handlebars.HelperRegistry
- Parameters:
helperSource
- The helper source. Required.- Returns:
- This handlebars object.
-
helper
- Specified by:
helper
in interfacecom.github.jknack.handlebars.HelperRegistry
-
helpers
- Specified by:
helpers
in interfacecom.github.jknack.handlebars.HelperRegistry
-
registerHelper
public <H> HandlebarsViewResolver registerHelper(String name, com.github.jknack.handlebars.Helper<H> helper) - Specified by:
registerHelper
in interfacecom.github.jknack.handlebars.HelperRegistry
-
registerHelperMissing
public <H> HandlebarsViewResolver registerHelperMissing(com.github.jknack.handlebars.Helper<H> helper) - Specified by:
registerHelperMissing
in interfacecom.github.jknack.handlebars.HelperRegistry
-
registerHelpers
- Specified by:
registerHelpers
in interfacecom.github.jknack.handlebars.HelperRegistry
- Throws:
Exception
-
registerHelpers
- Specified by:
registerHelpers
in interfacecom.github.jknack.handlebars.HelperRegistry
- Throws:
Exception
-
registerHelpers
- Specified by:
registerHelpers
in interfacecom.github.jknack.handlebars.HelperRegistry
- Throws:
Exception
-
registerHelpers
- Specified by:
registerHelpers
in interfacecom.github.jknack.handlebars.HelperRegistry
- Throws:
Exception
-
registerHelpers
- Specified by:
registerHelpers
in interfacecom.github.jknack.handlebars.HelperRegistry
- Throws:
IOException
-
withoutMessageHelper
Same assetRegisterMessageHelper(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 onMessageSource
) should be registered. Default is: true.- Parameters:
registerMessageHelper
- True, if the message helper (based onMessageSource
) should be registered. Default is: true.
-
setBindI18nToMessageSource
public void setBindI18nToMessageSource(boolean bindI18nToMessageSource) - Parameters:
bindI18nToMessageSource
- If true, the i18n helpers will use aMessageSource
instead of a plainResourceBundle
. 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 classorg.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
- Specified by:
decorator
in interfacecom.github.jknack.handlebars.HelperRegistry
-
registerDecorator
public HandlebarsViewResolver registerDecorator(String name, com.github.jknack.handlebars.Decorator decorator) - Specified by:
registerDecorator
in interfacecom.github.jknack.handlebars.HelperRegistry
-
setCharset
- Specified by:
setCharset
in interfacecom.github.jknack.handlebars.HelperRegistry
-