net.sf.click.extras.service
Class FreemarkerTemplateService

java.lang.Object
  extended bynet.sf.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="net.sf.click.examples.page"/>

     <template-service classname="net.sf.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>
 

Author:
Malcolm Edgar

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.
 
Constructor Summary
FreemarkerTemplateService()
           
 
Method Summary
 int getCacheDuration()
          Return the template cache duration in seconds to use when the application is in "productin" or "profile" mode.
 void onDestroy()
           
 void onInit(ServletContext servletContext)
           
 void renderTemplate(Page page, Map model, Writer writer)
           
 void renderTemplate(String templatePath, Map model, Writer writer)
           
 void setCacheDuration(int cacheDuration)
          Return the template cache duration in seconds to use when the application is in "productin" or "profile" mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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.

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 model,
                           Writer writer)
                    throws Exception
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:
Exception - if an error occurs
See Also:
TemplateService.renderTemplate(Page, Map, Writer)

renderTemplate

public void renderTemplate(String templatePath,
                           Map model,
                           Writer writer)
                    throws Exception
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:
Exception - if an 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 "productin" 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 "productin" or "profile" mode.

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