001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.servlet.view;
018:
019: /**
020: * Abstract base class for template view resolvers,
021: * in particular for Velocity and FreeMarker views.
022: *
023: * <p>Provides a convenient way to specify {@link AbstractTemplateView}'s
024: * exposure flags for request attributes, session attributes,
025: * and Spring's macro helpers.
026: *
027: * @author Juergen Hoeller
028: * @since 1.1
029: * @see AbstractTemplateView
030: * @see org.springframework.web.servlet.view.velocity.VelocityViewResolver
031: * @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver
032: */
033: public class AbstractTemplateViewResolver extends UrlBasedViewResolver {
034:
035: private boolean exposeRequestAttributes = false;
036:
037: private boolean allowRequestOverride = false;
038:
039: private boolean exposeSessionAttributes = false;
040:
041: private boolean allowSessionOverride = false;
042:
043: private boolean exposeSpringMacroHelpers = false;
044:
045: protected Class requiredViewClass() {
046: return AbstractTemplateView.class;
047: }
048:
049: /**
050: * Set whether all request attributes should be added to the
051: * model prior to merging with the template. Default is "false".
052: * @see AbstractTemplateView#setExposeRequestAttributes
053: */
054: public void setExposeRequestAttributes(
055: boolean exposeRequestAttributes) {
056: this .exposeRequestAttributes = exposeRequestAttributes;
057: }
058:
059: /**
060: * Set whether HttpServletRequest attributes are allowed to override (hide)
061: * controller generated model attributes of the same name. Default is "false",
062: * which causes an exception to be thrown if request attributes of the same
063: * name as model attributes are found.
064: * @see AbstractTemplateView#setAllowRequestOverride
065: */
066: public void setAllowRequestOverride(boolean allowRequestOverride) {
067: this .allowRequestOverride = allowRequestOverride;
068: }
069:
070: /**
071: * Set whether all HttpSession attributes should be added to the
072: * model prior to merging with the template. Default is "false".
073: * @see AbstractTemplateView#setExposeSessionAttributes
074: */
075: public void setExposeSessionAttributes(
076: boolean exposeSessionAttributes) {
077: this .exposeSessionAttributes = exposeSessionAttributes;
078: }
079:
080: /**
081: * Set whether HttpSession attributes are allowed to override (hide)
082: * controller generated model attributes of the same name. Default is "false",
083: * which causes an exception to be thrown if session attributes of the same
084: * name as model attributes are found.
085: * @see AbstractTemplateView#setAllowSessionOverride
086: */
087: public void setAllowSessionOverride(boolean allowSessionOverride) {
088: this .allowSessionOverride = allowSessionOverride;
089: }
090:
091: /**
092: * Set whether to expose a RequestContext for use by Spring's macro library,
093: * under the name "springMacroRequestContext". Default is "false".
094: * @see AbstractTemplateView#setExposeSpringMacroHelpers
095: */
096: public void setExposeSpringMacroHelpers(
097: boolean exposeSpringMacroHelpers) {
098: this .exposeSpringMacroHelpers = exposeSpringMacroHelpers;
099: }
100:
101: protected AbstractUrlBasedView buildView(String viewName)
102: throws Exception {
103: AbstractTemplateView view = (AbstractTemplateView) super
104: .buildView(viewName);
105: view.setExposeRequestAttributes(this.exposeRequestAttributes);
106: view.setAllowRequestOverride(this.allowRequestOverride);
107: view.setExposeSessionAttributes(this.exposeSessionAttributes);
108: view.setAllowSessionOverride(this.allowSessionOverride);
109: view.setExposeSpringMacroHelpers(this.exposeSpringMacroHelpers);
110: return view;
111: }
112:
113: }
|