org.springframework.web.servlet.view
abstract public class: AbstractTemplateView [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
All Implemented Interfaces:
InitializingBean, View, BeanNameAware, ServletContextAware, ApplicationContextAware
Direct Known Subclasses:
FreeMarkerView, VelocityToolboxView, VelocityLayoutView, VelocityView
Adapter base class for template-based view technologies such as
Velocity and FreeMarker, with the ability to use request and session
attributes in their model and the option to expose helper objects
for Spring's Velocity/FreeMarker macro library.
JSP/JSTL and other view technologies automatically have access to the
HttpServletRequest object and thereby the request/session attributes
for the current user. Furthermore, they are able to create and cache
helper objects as request attributes themselves.
| Field Summary |
|---|
| public static final String | SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE | Variable name of the RequestContext instance in the template model,
available to Spring's macros: e.g. for creating BindStatus objects. |
| 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.AbstractTemplateView Detail: |
protected void applyContentType(HttpServletResponse response) {
boolean apply = true;
if (responseGetContentTypeAvailable) {
try {
apply = (response.getContentType() == null);
}
catch (Throwable ex) {
// Probably Servlet 2.4 API present but not implemented.
// Behave like on Servlet 2.3...
apply = true;
}
}
if (apply) {
response.setContentType(getContentType());
}
}
Apply this view's content type as specified in the "contentType"
bean property to the given response.
When running on Servlet 2.4, only applies the view's contentType
if no content type has been set on the response before. This allows
handlers to override the default content type beforehand. |
protected final void renderMergedOutputModel(Map model,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (this.exposeRequestAttributes) {
for (Enumeration en = request.getAttributeNames(); en.hasMoreElements();) {
String attribute = (String) en.nextElement();
if (model.containsKey(attribute) && !this.allowRequestOverride) {
throw new ServletException("Cannot expose request attribute '" + attribute +
"' because of an existing model object of the same name");
}
Object attributeValue = request.getAttribute(attribute);
if (logger.isDebugEnabled()) {
logger.debug("Exposing request attribute '" + attribute +
"' with value [" + attributeValue + "] to model");
}
model.put(attribute, attributeValue);
}
}
if (this.exposeSessionAttributes) {
HttpSession session = request.getSession(false);
if (session != null) {
for (Enumeration en = session.getAttributeNames(); en.hasMoreElements();) {
String attribute = (String) en.nextElement();
if (model.containsKey(attribute) && !this.allowSessionOverride) {
throw new ServletException("Cannot expose session attribute '" + attribute +
"' because of an existing model object of the same name");
}
Object attributeValue = session.getAttribute(attribute);
if (logger.isDebugEnabled()) {
logger.debug("Exposing session attribute '" + attribute +
"' with value [" + attributeValue + "] to model");
}
model.put(attribute, attributeValue);
}
}
}
if (this.exposeSpringMacroHelpers) {
if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) {
throw new ServletException(
"Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE +
"' because of an existing model object of the same name");
}
// Expose RequestContext instance for Spring macros.
model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE,
new RequestContext(request, getServletContext(), model));
}
applyContentType(response);
renderMergedTemplateModel(model, request, response);
}
|
abstract protected void renderMergedTemplateModel(Map model,
HttpServletRequest request,
HttpServletResponse response) throws Exception
Subclasses must implement this method to actually render the view. |
public void setAllowRequestOverride(boolean allowRequestOverride) {
this.allowRequestOverride = allowRequestOverride;
}
Set whether HttpServletRequest attributes are allowed to override (hide)
controller generated model attributes of the same name. Default is "false",
which causes an exception to be thrown if request attributes of the same
name as model attributes are found. |
public void setAllowSessionOverride(boolean allowSessionOverride) {
this.allowSessionOverride = allowSessionOverride;
}
Set whether HttpSession attributes are allowed to override (hide)
controller generated model attributes of the same name. Default is "false",
which causes an exception to be thrown if session attributes of the same
name as model attributes are found. |
public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
this.exposeRequestAttributes = exposeRequestAttributes;
}
Set whether all request attributes should be added to the
model prior to merging with the template. Default is "false". |
public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
this.exposeSessionAttributes = exposeSessionAttributes;
}
Set whether all HttpSession attributes should be added to the
model prior to merging with the template. Default is "false". |
public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
}
Set whether to expose a RequestContext for use by Spring's macro library,
under the name "springMacroRequestContext". Default is "true".
Currently needed for Spring's Velocity and FreeMarker default macros.
Note that this is not required for templates that use HTML
forms unless you wish to take advantage of the Spring helper macros. |