001/* 002 * Copyright 2002-2020 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 * https://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 017package org.springframework.web.servlet.view; 018 019/** 020 * Abstract base class for template view resolvers, in particular for FreeMarker views. 021 * 022 * <p>Provides a convenient way to specify {@link AbstractTemplateView}'s exposure 023 * flags for request attributes, session attributes, and Spring's macro helpers. 024 * 025 * @author Juergen Hoeller 026 * @since 1.1 027 * @see AbstractTemplateView 028 * @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver 029 */ 030public abstract class AbstractTemplateViewResolver extends UrlBasedViewResolver { 031 032 private boolean exposeRequestAttributes = false; 033 034 private boolean allowRequestOverride = false; 035 036 private boolean exposeSessionAttributes = false; 037 038 private boolean allowSessionOverride = false; 039 040 private boolean exposeSpringMacroHelpers = true; 041 042 043 @Override 044 protected Class<?> requiredViewClass() { 045 return AbstractTemplateView.class; 046 } 047 048 /** 049 * Set whether all request attributes should be added to the 050 * model prior to merging with the template. Default is "false". 051 * @see AbstractTemplateView#setExposeRequestAttributes 052 */ 053 public void setExposeRequestAttributes(boolean exposeRequestAttributes) { 054 this.exposeRequestAttributes = exposeRequestAttributes; 055 } 056 057 /** 058 * Set whether HttpServletRequest attributes are allowed to override (hide) 059 * controller generated model attributes of the same name. Default is "false", 060 * which causes an exception to be thrown if request attributes of the same 061 * name as model attributes are found. 062 * @see AbstractTemplateView#setAllowRequestOverride 063 */ 064 public void setAllowRequestOverride(boolean allowRequestOverride) { 065 this.allowRequestOverride = allowRequestOverride; 066 } 067 068 /** 069 * Set whether all HttpSession attributes should be added to the 070 * model prior to merging with the template. Default is "false". 071 * @see AbstractTemplateView#setExposeSessionAttributes 072 */ 073 public void setExposeSessionAttributes(boolean exposeSessionAttributes) { 074 this.exposeSessionAttributes = exposeSessionAttributes; 075 } 076 077 /** 078 * Set whether HttpSession attributes are allowed to override (hide) 079 * controller generated model attributes of the same name. Default is "false", 080 * which causes an exception to be thrown if session attributes of the same 081 * name as model attributes are found. 082 * @see AbstractTemplateView#setAllowSessionOverride 083 */ 084 public void setAllowSessionOverride(boolean allowSessionOverride) { 085 this.allowSessionOverride = allowSessionOverride; 086 } 087 088 /** 089 * Set whether to expose a RequestContext for use by Spring's macro library, 090 * under the name "springMacroRequestContext". Default is "true". 091 * @see AbstractTemplateView#setExposeSpringMacroHelpers 092 */ 093 public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) { 094 this.exposeSpringMacroHelpers = exposeSpringMacroHelpers; 095 } 096 097 098 @Override 099 protected AbstractUrlBasedView buildView(String viewName) throws Exception { 100 AbstractTemplateView view = (AbstractTemplateView) super.buildView(viewName); 101 view.setExposeRequestAttributes(this.exposeRequestAttributes); 102 view.setAllowRequestOverride(this.allowRequestOverride); 103 view.setExposeSessionAttributes(this.exposeSessionAttributes); 104 view.setAllowSessionOverride(this.allowSessionOverride); 105 view.setExposeSpringMacroHelpers(this.exposeSpringMacroHelpers); 106 return view; 107 } 108 109}