|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.AbstractUrlBasedView
org.springframework.web.servlet.view.AbstractTemplateView
org.springframework.web.servlet.view.freemarker.FreeMarkerView
public class FreeMarkerView
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.
See setConfiguration(Configuration)
for more details on the impacts of this approach.
Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
AbstractUrlBasedView.setUrl(java.lang.String)
,
AbstractTemplateView.setExposeSpringMacroHelpers(boolean)
,
setEncoding(java.lang.String)
,
setConfiguration(Configuration)
,
FreeMarkerConfig
,
FreeMarkerConfigurer
Nested Class Summary | |
---|---|
private class |
FreeMarkerView.DelegatingServletConfig
Internal implementation of the FreeMarkerView.DelegatingServletConfig interface,
to be passed to the servlet adapter. |
private static class |
FreeMarkerView.GenericServletAdapter
Simple adapter class that extends GenericServlet . |
Field Summary | |
---|---|
private Configuration |
configuration
|
private java.lang.String |
encoding
|
private ServletContextHashModel |
servletContextHashModel
|
private TaglibFactory |
taglibFactory
|
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 |
Fields inherited from interface org.springframework.web.servlet.View |
---|
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE |
Constructor Summary | |
---|---|
FreeMarkerView()
|
Method Summary | |
---|---|
protected FreeMarkerConfig |
autodetectConfiguration()
Autodetect a FreeMarkerConfig object via the ApplicationContext. |
private HttpSessionHashModel |
buildSessionModel(HttpServletRequest request,
HttpServletResponse response)
Build a FreeMarker HttpSessionHashModel for the given request,
detecting whether a session already exists and reacting accordingly. |
protected SimpleHash |
buildTemplateModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Build a FreeMarker template model for the given model Map. |
boolean |
checkResource(java.util.Locale locale)
Check that the FreeMarker template used for this view exists and is valid. |
protected void |
doRender(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Render the FreeMarker view to the given response, using the given model map which contains the complete template model to use. |
protected void |
exposeHelpers(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request)
Expose helpers unique to each rendering operation. |
protected Configuration |
getConfiguration()
Return the FreeMarker configuration used by this view. |
protected java.lang.String |
getEncoding()
Return the encoding for the FreeMarker template. |
protected ObjectWrapper |
getObjectWrapper()
Return the configured FreeMarker ObjectWrapper , or the
default wrapper if none specified. |
protected Template |
getTemplate(java.util.Locale locale)
Retrieve the FreeMarker template for the given locale, to be rendering by this view. |
protected Template |
getTemplate(java.lang.String name,
java.util.Locale locale)
Retrieve the FreeMarker template specified by the given name, using the encoding specified by the "encoding" bean property. |
protected void |
initServletContext(ServletContext servletContext)
Invoked on startup. |
protected void |
processTemplate(Template template,
SimpleHash model,
HttpServletResponse response)
Process the FreeMarker template to the servlet response. |
protected void |
renderMergedTemplateModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Process the model map by merging it with the FreeMarker template. |
void |
setConfiguration(Configuration configuration)
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 |
---|
applyContentType, renderMergedOutputModel, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpers |
Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView |
---|
afterPropertiesSet, getUrl, isUrlRequired, setUrl, toString |
Methods inherited from class org.springframework.web.servlet.view.AbstractView |
---|
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposePathVariables, setRequestContextAttribute, writeToResponse |
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport |
---|
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, isContextRequired, setServletContext |
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport |
---|
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private java.lang.String encoding
private Configuration configuration
private TaglibFactory taglibFactory
private ServletContextHashModel servletContextHashModel
Constructor Detail |
---|
public FreeMarkerView()
Method Detail |
---|
public void setEncoding(java.lang.String encoding)
Specify the encoding in the FreeMarker Configuration rather than per template if all your templates share a common encoding.
protected java.lang.String getEncoding()
public void setConfiguration(Configuration configuration)
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.
Note: using this method will cause a new instance of TaglibFactory
to created for every single FreeMarkerView
instance. This can be quite expensive
in terms of memory and initial CPU usage. In production it is recommended that you use
a FreeMarkerConfig
which exposes a single shared TaglibFactory
.
protected Configuration getConfiguration()
protected void initServletContext(ServletContext servletContext) throws BeansException
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.
initServletContext
in class WebApplicationObjectSupport
servletContext
- the ServletContext that this application object runs in
(never null
)
BeansException
freemarker.cache.TemplateCache#getTemplate
protected FreeMarkerConfig autodetectConfiguration() throws BeansException
FreeMarkerConfig
object via the ApplicationContext.
BeansException
- if no Configuration instance could be foundApplicationObjectSupport.getApplicationContext()
,
setConfiguration(Configuration)
protected ObjectWrapper getObjectWrapper()
ObjectWrapper
, or the
default wrapper
if none specified.
freemarker.template.Configuration#getObjectWrapper()
public boolean checkResource(java.util.Locale locale) throws java.lang.Exception
Can be overridden to customize the behavior, for example in case of multiple templates to be rendered into a single view.
checkResource
in class AbstractUrlBasedView
locale
- the desired Locale that we're looking for
true
if the resource exists (or is assumed to exist);
false
if we know that it does not exist
java.lang.Exception
- if the resource exists but is invalid (e.g. could not be parsed)protected void renderMergedTemplateModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
This method can be overridden if custom behavior is needed.
renderMergedTemplateModel
in class AbstractTemplateView
model
- combined output Map, with request attributes and
session attributes merged into it if requiredrequest
- current HTTP requestresponse
- current HTTP response
java.lang.Exception
- if rendering failedprotected void exposeHelpers(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request) throws java.lang.Exception
Called by renderMergedTemplateModel
. The default implementation
is empty. This method can be overridden to add custom helpers to the model.
model
- The model that will be passed to the template at merge timerequest
- current HTTP request
java.lang.Exception
- if there's a fatal error while we're adding information to the contextrenderMergedTemplateModel(java.util.Map, HttpServletRequest, HttpServletResponse)
protected void doRender(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
The default implementation renders the template specified by the "url"
bean property, retrieved via getTemplate
. It delegates to the
processTemplate
method to merge the template instance with
the given template model.
Adds the standard Freemarker hash models to the model: request parameters, request, session and application (ServletContext), as well as the JSP tag library hash model.
Can be overridden to customize the behavior, for example to render multiple templates into a single view.
model
- the model to use for renderingrequest
- current HTTP requestresponse
- current servlet response
java.io.IOException
- if the template file could not be retrieved
java.lang.Exception
- if rendering failedAbstractUrlBasedView.setUrl(java.lang.String)
,
RequestContextUtils.getLocale(HttpServletRequest)
,
getTemplate(java.util.Locale)
,
processTemplate(Template, SimpleHash, HttpServletResponse)
,
freemarker.ext.servlet.FreemarkerServlet
protected SimpleHash buildTemplateModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response)
The default implementation builds a AllHttpScopesHashModel
.
model
- the model to use for renderingrequest
- current HTTP requestresponse
- current servlet response
SimpleHash
or subclass thereofprivate HttpSessionHashModel buildSessionModel(HttpServletRequest request, HttpServletResponse response)
HttpSessionHashModel
for the given request,
detecting whether a session already exists and reacting accordingly.
request
- current HTTP requestresponse
- current servlet response
protected Template getTemplate(java.util.Locale locale) throws java.io.IOException
By default, the template specified by the "url" bean property will be retrieved.
locale
- the current locale
java.io.IOException
- if the template file could not be retrievedAbstractUrlBasedView.setUrl(java.lang.String)
,
getTemplate(String, java.util.Locale)
protected Template getTemplate(java.lang.String name, java.util.Locale locale) throws java.io.IOException
Can be called by subclasses to retrieve a specific template, for example to render multiple templates into a single view.
name
- the file name of the desired templatelocale
- the current locale
java.io.IOException
- if the template file could not be retrievedprotected void processTemplate(Template template, SimpleHash model, HttpServletResponse response) throws java.io.IOException, TemplateException
Can be overridden to customize the behavior.
template
- the template to processmodel
- the model for the templateresponse
- servlet response (use this to get the OutputStream or Writer)
java.io.IOException
- if the template file could not be retrieved
TemplateException
- if thrown by FreeMarkerfreemarker.template.Template#process(Object, java.io.Writer)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |