org.apache.click.extras.service
Class FreemarkerTemplateService

java.lang.Object
  extended by org.apache.click.extras.service.FreemarkerTemplateService
All Implemented Interfaces:
TemplateService

public class FreemarkerTemplateService
extends Object
implements TemplateService

Provides a Freemarker TemplateService class.

Configuration

To configure the Freemarker TemplateService add the following element to your click.xml configuration file.
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <click-app charset="UTF-8">

     <pages package="org.apache.click.examples.page"/>

     <template-service classname="org.apache.click.extras.service.FreemarkerTemplateService"/>

 </click-app> 
Please note that Click ships with a default error.htm that is tailored for Velocity.

If you switch to Freemarker replace the default error.htm with the one shown below.

To ensure Click uses your template instead of the default one, copy/paste the template below to the web application path /click/error.htm. Click won't override your custom template.

 <html>
   <head>
     <title>Error Page</title>
     <style  type='text/css'>
       body, table, td {
       font-family: arial, sans-serif;
       font-size: 12px;
     }
     td.header {
       color: white;
       background: navy;
     }
     .errorReport {
       display: none;
     }
     a {
       color: blue;
     }
     </style>
     <script type='text/javascript'>
       function displayError() {
         errorReport.style.display = 'block';
       }
     </script>
   </head>

   <body>
     <h1>Error Page</h1>

     <#if errorReport??>
       The application encountered an unexpected error.
       <p/>
       To return to the application click <a href="${context}">here</a>.
       <p/>

       <#if mode != "production">
         To view the error details click <a href="#" onclick="displayError();">here</a>.
         <p/>
         ${errorReport}
         <p/>
       </#if>
     </#if>

   </body>
 </html>
 


Field Summary
protected  int cacheDuration
          The production/profile mode cache duration in seconds.
protected  ConfigService configService
          The application configuration service.
protected  freemarker.template.Configuration configuration
          The Freemarker engine configuration.
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 NOT_FOUND_PAGE_PATH
          The click not found page template path.
 
Constructor Summary
FreemarkerTemplateService()
           
 
Method Summary
 int getCacheDuration()
          Return the template cache duration in seconds to use when the application is in "production" or "profile" mode.
 void onDestroy()
           
 void onInit(ServletContext servletContext)
           
 void renderTemplate(Page page, Map<String,?> model, Writer writer)
           
 void renderTemplate(String templatePath, Map<String,?> model, Writer writer)
           
 void setCacheDuration(int cacheDuration)
          Return the template cache duration in seconds to use when the application is in "production" or "profile" mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ERROR_PAGE_PATH

protected static final String ERROR_PAGE_PATH
The click error page template path.

See Also:
Constant Field Values

NOT_FOUND_PAGE_PATH

protected static final String NOT_FOUND_PAGE_PATH
The click not found page template path.

See Also:
Constant Field Values

configuration

protected freemarker.template.Configuration configuration
The Freemarker engine configuration.


cacheDuration

protected int cacheDuration
The production/profile mode cache duration in seconds. The default value is 24 hours.


configService

protected ConfigService configService
The application configuration service.


deployedErrorTemplate

protected boolean deployedErrorTemplate
The /click/error.htm page template has been deployed.


deployedNotFoundTemplate

protected boolean deployedNotFoundTemplate
The /click/not-found.htm page template has been deployed.

Constructor Detail

FreemarkerTemplateService

public FreemarkerTemplateService()
Method Detail

onInit

public void onInit(ServletContext servletContext)
            throws Exception
Specified by:
onInit in interface TemplateService
Parameters:
servletContext - the application servlet context
Throws:
Exception - if an error occurs initializing the Template Service
See Also:
TemplateService.onInit(javax.servlet.ServletContext)

onDestroy

public void onDestroy()
Specified by:
onDestroy in interface TemplateService
See Also:
TemplateService.onDestroy()

renderTemplate

public void renderTemplate(Page page,
                           Map<String,?> model,
                           Writer writer)
                    throws IOException,
                           TemplateException
Specified by:
renderTemplate in interface TemplateService
Parameters:
page - the page template to render
model - the model to merge with the template and render
writer - the writer to send the merged template and model data to
Throws:
IOException - if an IO error occurs
TemplateException - if template error occurs
See Also:
TemplateService.renderTemplate(Page, Map, Writer)

renderTemplate

public void renderTemplate(String templatePath,
                           Map<String,?> model,
                           Writer writer)
                    throws IOException,
                           TemplateException
Specified by:
renderTemplate in interface TemplateService
Parameters:
templatePath - the path of the template to render
model - the model to merge with the template and render
writer - the writer to send the merged template and model data to
Throws:
IOException - if an IO error occurs
TemplateException - if template error occurs
See Also:
TemplateService.renderTemplate(String, Map, Writer)

getCacheDuration

public int getCacheDuration()
Return the template cache duration in seconds to use when the application is in "production" or "profile" mode.

Returns:
the cacheDuration the template cache duration in seconds

setCacheDuration

public void setCacheDuration(int cacheDuration)
Return the template cache duration in seconds to use when the application is in "production" or "profile" mode.

Parameters:
cacheDuration - the template cache duration in seconds to set