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, 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 */ 033public abstract 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 = true; 044 045 046 @Override 047 protected Class<?> requiredViewClass() { 048 return AbstractTemplateView.class; 049 } 050 051 /** 052 * Set whether all request attributes should be added to the 053 * model prior to merging with the template. Default is "false". 054 * @see AbstractTemplateView#setExposeRequestAttributes 055 */ 056 public void setExposeRequestAttributes(boolean exposeRequestAttributes) { 057 this.exposeRequestAttributes = exposeRequestAttributes; 058 } 059 060 /** 061 * Set whether HttpServletRequest attributes are allowed to override (hide) 062 * controller generated model attributes of the same name. Default is "false", 063 * which causes an exception to be thrown if request attributes of the same 064 * name as model attributes are found. 065 * @see AbstractTemplateView#setAllowRequestOverride 066 */ 067 public void setAllowRequestOverride(boolean allowRequestOverride) { 068 this.allowRequestOverride = allowRequestOverride; 069 } 070 071 /** 072 * Set whether all HttpSession attributes should be added to the 073 * model prior to merging with the template. Default is "false". 074 * @see AbstractTemplateView#setExposeSessionAttributes 075 */ 076 public void setExposeSessionAttributes(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 "true". 094 * @see AbstractTemplateView#setExposeSpringMacroHelpers 095 */ 096 public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) { 097 this.exposeSpringMacroHelpers = exposeSpringMacroHelpers; 098 } 099 100 101 @Override 102 protected AbstractUrlBasedView buildView(String viewName) throws Exception { 103 AbstractTemplateView view = (AbstractTemplateView) super.buildView(viewName); 104 view.setExposeRequestAttributes(this.exposeRequestAttributes); 105 view.setAllowRequestOverride(this.allowRequestOverride); 106 view.setExposeSessionAttributes(this.exposeSessionAttributes); 107 view.setAllowSessionOverride(this.allowSessionOverride); 108 view.setExposeSpringMacroHelpers(this.exposeSpringMacroHelpers); 109 return view; 110 } 111 112}