org.springframework.web.servlet.view.velocity
public class: VelocityLayoutView [javadoc |
source]
java.lang.Object
org.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.velocity.VelocityView
org.springframework.web.servlet.view.velocity.VelocityToolboxView
org.springframework.web.servlet.view.velocity.VelocityLayoutView
All Implemented Interfaces:
InitializingBean, View, BeanNameAware, ServletContextAware, ApplicationContextAware
VelocityLayoutView emulates the functionality offered by Velocity's
VelocityLayoutServlet to ease page composition from different templates.
The url property should be set to the content template
for the view, and the layout template location should be specified as
layoutUrl property. A view can override the configured
layout template location by setting the appropriate key (the default
is "layout") in the content template.
When the view is rendered, the VelocityContext is first merged with
the content template (specified by the url property) and
then merged with the layout template to produce the final output.
The layout template can include the screen content through a
VelocityContext variable (the default is "screen_content").
At runtime, this variable will contain the rendered content template.
| Field Summary |
|---|
| public static final String | DEFAULT_LAYOUT_URL | The default layout url . |
| public static final String | DEFAULT_LAYOUT_KEY | The default layout key . |
| public static final String | DEFAULT_SCREEN_CONTENT_KEY | The default screen content key . |
| Methods from org.springframework.web.servlet.view.velocity.VelocityView: |
|---|
|
autodetectVelocityEngine, checkTemplate, createVelocityContext, createVelocityContext, doRender, exposeHelpers, exposeHelpers, exposeHelpers, exposeToolAttributes, getEncoding, getTemplate, getTemplate, getVelocityEngine, initApplicationContext, initTool, isCacheTemplate, mergeTemplate, renderMergedTemplateModel, setCacheTemplate, setDateToolAttribute, setEncoding, setNumberToolAttribute, setToolAttributes, setVelocityEngine, setVelocityFormatterAttribute |
| Methods from org.springframework.web.servlet.view.AbstractView: |
|---|
|
addStaticAttribute, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, prepareResponse, render, renderMergedOutputModel, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute, toString, writeToResponse |
| Method from org.springframework.web.servlet.view.velocity.VelocityLayoutView Detail: |
protected void checkTemplate() throws ApplicationContextException {
super.checkTemplate();
try {
// Check that we can get the template, even if we might subsequently get it again.
getTemplate(this.layoutUrl);
}
catch (ResourceNotFoundException ex) {
throw new ApplicationContextException("Cannot find Velocity template for URL [" + this.layoutUrl +
"]: Did you specify the correct resource loader path?", ex);
}
catch (Exception ex) {
throw new ApplicationContextException(
"Could not load Velocity template for URL [" + this.layoutUrl + "]", ex);
}
}
Overrides VelocityView.checkTemplate() to additionally check
that both the layout template and the screen content template can be loaded.
Note that during rendering of the screen content, the layout template
can be changed which may invalidate any early checking done here. |
protected void doRender(Context context,
HttpServletResponse response) throws Exception {
renderScreenContent(context);
// Velocity context now includes any mappings that were defined
// (via #set) in screen content template.
// The screen template can overrule the layout by doing
// #set( $layout = "MyLayout.vm" )
String layoutUrlToUse = (String) context.get(this.layoutKey);
if (layoutUrlToUse != null) {
if (logger.isDebugEnabled()) {
logger.debug("Screen content template has requested layout [" + layoutUrlToUse + "]");
}
}
else {
// No explicit layout URL given - > use default layout of this view.
layoutUrlToUse = this.layoutUrl;
}
mergeTemplate(getTemplate(layoutUrlToUse), context, response);
}
Overrides the normal rendering process in order to pre-process the Context,
merging it with the screen template into a single value (identified by the
value of screenContentKey). The layout template is then merged with the
modified Context in the super class. |
public void setLayoutKey(String layoutKey) {
this.layoutKey = layoutKey;
}
Set the context key used to specify an alternate layout to be used instead
of the default layout. Screen content templates can override the layout
template that they wish to be wrapped with by setting this value in the
template, for example:
#set( $layout = "MyLayout.vm" )
Default key is "layout" , as illustrated above. |
public void setLayoutUrl(String layoutUrl) {
this.layoutUrl = layoutUrl;
}
Set the layout template to use. Default is "layout.vm" . |
public void setScreenContentKey(String screenContentKey) {
this.screenContentKey = screenContentKey;
}
|