|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.service.VelocityTemplateService
public class VelocityTemplateService
Provides a Velocity TemplateService class.
Velocity provides a simple to use, but powerful and performant templating engine for the Click Framework. The Velocity templating engine is configured and accessed by this VelocityTemplateService class. Velocity is the default templating engine used by Click and the Velocity class dependencies are included in the standard Click JAR file. You can also instruct Click to use a different template service implementation. Please seeTemplateService
for more details.
To see how to use the Velocity templating language please see the
Velocity Users Guide.
<template-service classname="org.apache.click.service.VelocityTemplateService"/>
resource.loader=webapp, class webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader webapp.resource.loader.cache=[true|false] #depending on application mode webapp.resource.loader.modificationCheckInterval=0 #depending on application mode class.resource.loader.class=org.apache.velocity.runtime.loader.ClasspathResourceLoader class.resource.loader.cache=[true|false] #depending on application mode class.resource.loader.modificationCheckInterval=0 #depending on application mode velocimacro.library.autoreload=[true|false] #depending on application mode velocimacro.library=click/VM_global_library.vmThis service uses the Velocity Tools WebappResourceLoader for loading templates. This avoids issues associate with using the Velocity FileResourceLoader on JEE application servers. See the Velocity Developer Guide for details about these properties. Note when the application is in trace mode the Velocity properties used will be logged on startup. If you want to add some of your own Velocity properties, or replace Click's properties, add a velocity.properties file in the WEB-INF directory. Click will automatically pick up this file and load these properties. As a example say we have our own Velocity macro library called mycorp.vm we can override the default velocimacro.library property by adding a WEB-INF/velocity.properties file to our web application. In this file we would then define the property as:
velocimacro.library=mycorp.vm
Note do not place Velocity macros under the WEB-INF directory as the Velocity
ResourceManager will not be able to load them.
The simplest way to set your own macro file is to add a file named macro.vm
under your web application's root directory. At startup Click will first check to see
if this file exists, and if it does it will use it instead of click/VM_global_library.vm.
webapp.resource.loader.cache=true webapp.resource.loader.modificationCheckInterval=0 class.resource.loader.cache=true class.resource.loader.modificationCheckInterval=0 velocimacro.library.autoreload=falseWhen running in these modes the
ConsoleLogService
will be configured
to use
webapp.resource.loader.cache=false class.resource.loader.cache=false velocimacro.library.autoreload=trueDisabling caching is useful for application development where you can edit page templates on a running application server and see the changes immediately. Please Note Velocity caching should be used for production as Velocity template reloading is much much slower and the process of parsing and introspecting templates and macros can use a lot of memory.
VelocityTemplateService.LogChuteAdapter
class is provided. This class wraps the
Click LogService with a Velocity LogChute so the Velocity runtime can
use it for logging messages to.
If you are using LogServices other than ConsoleLogService
you will
probably configure that service to filter out Velocity's verbose INFO
level messages.
Nested Class Summary | |
---|---|
static class |
VelocityTemplateService.LogChuteAdapter
Provides a Velocity LogChute adapter class around the application log service to enable the Velocity Runtime to log to the application LogService. |
Field Summary | |
---|---|
protected ConfigService |
configService
The application configuration service. |
protected static String |
DEFAULT_TEMPLATE_PROPS
The default velocity properties filename: "/WEB-INF/velocity.properties". |
protected boolean |
deployedErrorTemplate
The /click/error.htm page template has been deployed. |
protected boolean |
deployedNotFoundTemplate
The /click/not-found.htm page template has been deployed. |
protected static String |
ERROR_PAGE_PATH
The click error page template path. |
protected static String |
MACRO_VM_FILE_NAME
The user supplied macro file name: "macro.vm". |
protected static String |
NOT_FOUND_PAGE_PATH
The click not found page template path. |
protected org.apache.velocity.app.VelocityEngine |
velocityEngine
The VelocityEngine instance. |
protected static String |
VM_FILE_PATH
The global Velocity macro file path: "/click/VM_global_library.vm". |
protected static int |
WRITER_BUFFER_SIZE
The Velocity writer buffer size. |
protected org.apache.velocity.util.SimplePool |
writerPool
Cache of velocity writers. |
Constructor Summary | |
---|---|
VelocityTemplateService()
|
Method Summary | |
---|---|
protected Integer |
getInitLogLevel()
Return the Velocity Engine initialization log level. |
protected Properties |
getInitProperties()
Return the Velocity Engine initialization properties. |
protected void |
internalRenderTemplate(String templatePath,
Page page,
Map<String,?> model,
Writer writer)
Provides the underlying Velocity template rendering. |
void |
onDestroy()
Destroy the TemplateService. |
void |
onInit(ServletContext servletContext)
Initialize the TemplateService with the given application configuration service instance. |
void |
renderTemplate(Page page,
Map<String,?> model,
Writer writer)
Render the given page to the writer. |
void |
renderTemplate(String templatePath,
Map<String,?> model,
Writer writer)
Render the given template and model to the writer. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final String DEFAULT_TEMPLATE_PROPS
protected static final String ERROR_PAGE_PATH
protected static final String MACRO_VM_FILE_NAME
protected static final String NOT_FOUND_PAGE_PATH
protected static final String VM_FILE_PATH
protected static final int WRITER_BUFFER_SIZE
protected ConfigService configService
protected boolean deployedErrorTemplate
protected boolean deployedNotFoundTemplate
protected org.apache.velocity.app.VelocityEngine velocityEngine
protected org.apache.velocity.util.SimplePool writerPool
Constructor Detail |
---|
public VelocityTemplateService()
Method Detail |
---|
public void onInit(ServletContext servletContext) throws Exception
TemplateService
ConfigService
by invoking
ClickUtils.getConfigService(javax.servlet.ServletContext)
onInit
in interface TemplateService
servletContext
- the application servlet velocityContext
Exception
- if an error occurs initializing the Template ServiceTemplateService.onInit(ServletContext)
public void onDestroy()
TemplateService
onDestroy
in interface TemplateService
TemplateService.onDestroy()
public void renderTemplate(Page page, Map<String,?> model, Writer writer) throws IOException, TemplateException
TemplateService
renderTemplate
in interface TemplateService
page
- the page template to rendermodel
- the model to merge with the template and renderwriter
- the writer to send the merged template and model data to
IOException
- if an IO error occurs
TemplateException
- if template error occursTemplateService.renderTemplate(Page, Map, Writer)
public void renderTemplate(String templatePath, Map<String,?> model, Writer writer) throws IOException, TemplateException
TemplateService
renderTemplate
in interface TemplateService
templatePath
- the path of the template to rendermodel
- the model to merge with the template and renderwriter
- the writer to send the merged template and model data to
IOException
- if an IO error occurs
TemplateException
- if an error occursTemplateService.renderTemplate(String, Map, Writer)
protected Integer getInitLogLevel()
protected Properties getInitProperties() throws MalformedURLException
MalformedURLException
- if a resource cannot be loadedprotected void internalRenderTemplate(String templatePath, Page page, Map<String,?> model, Writer writer) throws IOException, TemplateException
templatePath
- the template path to renderpage
- the page template to rendermodel
- the model to merge with the template and renderwriter
- the writer to send the merged template and model data to
IOException
- if an IO error occurs
TemplateException
- if template error occurs
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |