org.springframework.web.servlet.view.freemarker
Class FreeMarkerView

java.lang.Object
  extended byorg.springframework.context.support.ApplicationObjectSupport
      extended byorg.springframework.web.context.support.WebApplicationObjectSupport
          extended byorg.springframework.web.servlet.view.AbstractView
              extended byorg.springframework.web.servlet.view.AbstractUrlBasedView
                  extended byorg.springframework.web.servlet.view.AbstractTemplateView
                      extended byorg.springframework.web.servlet.view.freemarker.FreeMarkerView
All Implemented Interfaces:
ApplicationContextAware, BeanNameAware, View

public class FreeMarkerView
extends AbstractTemplateView

View using the FreeMarker template engine.

Exposes the following JavaBean properties:

Depends on a single FreeMarkerConfig object such as FreeMarkerConfigurer being accessible in the current web application context, with any bean name. Alternatively, you can set the Freemarker Configuration object as bean property.

Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.

Since:
3/3/2004
Author:
Darren Davison, Juergen Hoeller
See Also:
AbstractUrlBasedView.setUrl(java.lang.String), AbstractTemplateView.setExposeSpringMacroHelpers(boolean), setEncoding(java.lang.String), setConfiguration(freemarker.template.Configuration), FreeMarkerConfig, FreeMarkerConfigurer

Field Summary
 
Fields inherited from class org.springframework.web.servlet.view.AbstractTemplateView
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
 
Fields inherited from class org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
FreeMarkerView()
           
 
Method Summary
protected  void exposeHelpers(java.util.Map model, javax.servlet.http.HttpServletRequest request)
          Expose helpers unique to each rendering operation.
protected  freemarker.template.Configuration getConfiguration()
          Return the FreeMarker configuration used by this view.
protected  java.lang.String getEncoding()
          Return the encoding for the FreeMarker template.
protected  freemarker.template.Template getTemplate(java.util.Locale locale)
          Retrieve the FreeMarker template for the given locale.
protected  void initApplicationContext()
          Invoked on startup.
protected  void processTemplate(freemarker.template.Template template, java.util.Map model, javax.servlet.http.HttpServletResponse response)
          Process the FreeMarker template to the servlet response.
protected  void renderMergedTemplateModel(java.util.Map model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Process the model map by merging it with the FreeMarker template.
 void setConfiguration(freemarker.template.Configuration configration)
          Set the FreeMarker Configuration to be used by this view.
 void setEncoding(java.lang.String encoding)
          Set the encoding of the FreeMarker template file.
 
Methods inherited from class org.springframework.web.servlet.view.AbstractTemplateView
renderMergedOutputModel, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpers
 
Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView
getUrl, setUrl
 
Methods inherited from class org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createRequestContext, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FreeMarkerView

public FreeMarkerView()
Method Detail

setEncoding

public void setEncoding(java.lang.String encoding)
Set the encoding of the FreeMarker template file. Default is determined by the FreeMarker Configuration: "ISO-8859-1" if not specified otherwise.

Specify the encoding in the FreeMarker Configuration rather than per template if all your templates share a common encoding.


getEncoding

protected java.lang.String getEncoding()
Return the encoding for the FreeMarker template.


setConfiguration

public void setConfiguration(freemarker.template.Configuration configration)
Set the FreeMarker Configuration to be used by this view. If this is not set, the default lookup will occur: A single FreeMarkerConfig is expected in the current web application context, with any bean name.

See Also:
FreeMarkerConfig

getConfiguration

protected freemarker.template.Configuration getConfiguration()
Return the FreeMarker configuration used by this view.


initApplicationContext

protected void initApplicationContext()
                               throws BeansException
Invoked on startup. Looks for a single FreeMarkerConfig bean to find the relevant Configuration for this factory.

Checks that the template for the default Locale can be found: FreeMarker will check non-Locale-specific templates if a locale-specific one is not found.

Overrides:
initApplicationContext in class AbstractUrlBasedView
Throws:
BeansException
See Also:
TemplateCache.getTemplate(java.lang.String, java.util.Locale, java.lang.String, boolean)

renderMergedTemplateModel

protected void renderMergedTemplateModel(java.util.Map model,
                                         javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response)
                                  throws java.lang.Exception
Process the model map by merging it with the FreeMarker template. Output is directed to the response. This method can be overridden if custom behavior is needed.

Specified by:
renderMergedTemplateModel in class AbstractTemplateView
Parameters:
model - combined output Map, with request attributes and session attributes merged into it if required
request - current HTTP request
response - current HTTP response
Throws:
java.lang.Exception - if rendering failed

getTemplate

protected freemarker.template.Template getTemplate(java.util.Locale locale)
                                            throws java.io.IOException
Retrieve the FreeMarker template for the given locale.

Parameters:
locale - the current locale
Returns:
the FreeMarker template to process
Throws:
java.io.IOException - if the template file could not be retrieved

exposeHelpers

protected void exposeHelpers(java.util.Map model,
                             javax.servlet.http.HttpServletRequest request)
                      throws java.lang.Exception
Expose helpers unique to each rendering operation. This is necessary so that different rendering operations can't overwrite each other's formats etc.

Called by renderMergedTemplateModel. The default implementations is empty. This method can be overridden to add custom helpers to the model.

Parameters:
model - The model that will be passed to the template at merge time
request - current HTTP request
Throws:
java.lang.Exception - if there's a fatal error while we're adding information to the context
See Also:
renderMergedTemplateModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

processTemplate

protected void processTemplate(freemarker.template.Template template,
                               java.util.Map model,
                               javax.servlet.http.HttpServletResponse response)
                        throws java.io.IOException,
                               freemarker.template.TemplateException
Process the FreeMarker template to the servlet response. Can be overridden to customize the behavior.

Parameters:
template - the template to process
model - the model for the template
response - servlet response (use this to get the OutputStream or Writer)
Throws:
java.io.IOException
freemarker.template.TemplateException
See Also:
Template.process(Object, java.io.Writer)


Copyright (C) 2003-2004 The Spring Framework Project.