View Javadoc
1   /*
2    * Copyright 2002-2015 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.view;
18  
19  import java.util.Enumeration;
20  import java.util.Map;
21  import javax.servlet.ServletException;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.http.HttpSession;
25  
26  import org.springframework.web.servlet.support.RequestContext;
27  
28  /**
29   * Adapter base class for template-based view technologies such as
30   * Velocity and FreeMarker, with the ability to use request and session
31   * attributes in their model and the option to expose helper objects
32   * for Spring's Velocity/FreeMarker macro library.
33   *
34   * <p>JSP/JSTL and other view technologies automatically have access to the
35   * HttpServletRequest object and thereby the request/session attributes
36   * for the current user. Furthermore, they are able to create and cache
37   * helper objects as request attributes themselves.
38   *
39   * @author Juergen Hoeller
40   * @author Darren Davison
41   * @since 1.0.2
42   * @see AbstractTemplateViewResolver
43   * @see org.springframework.web.servlet.view.velocity.VelocityView
44   * @see org.springframework.web.servlet.view.freemarker.FreeMarkerView
45   */
46  public abstract class AbstractTemplateView extends AbstractUrlBasedView {
47  
48  	/**
49  	 * Variable name of the RequestContext instance in the template model,
50  	 * available to Spring's macros: e.g. for creating BindStatus objects.
51  	 */
52  	public static final String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE = "springMacroRequestContext";
53  
54  
55  	private boolean exposeRequestAttributes = false;
56  
57  	private boolean allowRequestOverride = false;
58  
59  	private boolean exposeSessionAttributes = false;
60  
61  	private boolean allowSessionOverride = false;
62  
63  	private boolean exposeSpringMacroHelpers = true;
64  
65  
66  	/**
67  	 * Set whether all request attributes should be added to the
68  	 * model prior to merging with the template. Default is "false".
69  	 */
70  	public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
71  		this.exposeRequestAttributes = exposeRequestAttributes;
72  	}
73  
74  	/**
75  	 * Set whether HttpServletRequest attributes are allowed to override (hide)
76  	 * controller generated model attributes of the same name. Default is "false",
77  	 * which causes an exception to be thrown if request attributes of the same
78  	 * name as model attributes are found.
79  	 */
80  	public void setAllowRequestOverride(boolean allowRequestOverride) {
81  		this.allowRequestOverride = allowRequestOverride;
82  	}
83  
84  	/**
85  	 * Set whether all HttpSession attributes should be added to the
86  	 * model prior to merging with the template. Default is "false".
87  	 */
88  	public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
89  		this.exposeSessionAttributes = exposeSessionAttributes;
90  	}
91  
92  	/**
93  	 * Set whether HttpSession attributes are allowed to override (hide)
94  	 * controller generated model attributes of the same name. Default is "false",
95  	 * which causes an exception to be thrown if session attributes of the same
96  	 * name as model attributes are found.
97  	 */
98  	public void setAllowSessionOverride(boolean allowSessionOverride) {
99  		this.allowSessionOverride = allowSessionOverride;
100 	}
101 
102 	/**
103 	 * Set whether to expose a RequestContext for use by Spring's macro library,
104 	 * under the name "springMacroRequestContext". Default is "true".
105 	 * <p>Currently needed for Spring's Velocity and FreeMarker default macros.
106 	 * Note that this is <i>not</i> required for templates that use HTML
107 	 * forms <i>unless</i> you wish to take advantage of the Spring helper macros.
108 	 * @see #SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
109 	 */
110 	public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
111 		this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
112 	}
113 
114 
115 	@Override
116 	protected final void renderMergedOutputModel(
117 			Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
118 
119 		if (this.exposeRequestAttributes) {
120 			for (Enumeration<String> en = request.getAttributeNames(); en.hasMoreElements();) {
121 				String attribute = en.nextElement();
122 				if (model.containsKey(attribute) && !this.allowRequestOverride) {
123 					throw new ServletException("Cannot expose request attribute '" + attribute +
124 						"' because of an existing model object of the same name");
125 				}
126 				Object attributeValue = request.getAttribute(attribute);
127 				if (logger.isDebugEnabled()) {
128 					logger.debug("Exposing request attribute '" + attribute +
129 							"' with value [" + attributeValue + "] to model");
130 				}
131 				model.put(attribute, attributeValue);
132 			}
133 		}
134 
135 		if (this.exposeSessionAttributes) {
136 			HttpSession session = request.getSession(false);
137 			if (session != null) {
138 				for (Enumeration<String> en = session.getAttributeNames(); en.hasMoreElements();) {
139 					String attribute = en.nextElement();
140 					if (model.containsKey(attribute) && !this.allowSessionOverride) {
141 						throw new ServletException("Cannot expose session attribute '" + attribute +
142 							"' because of an existing model object of the same name");
143 					}
144 					Object attributeValue = session.getAttribute(attribute);
145 					if (logger.isDebugEnabled()) {
146 						logger.debug("Exposing session attribute '" + attribute +
147 								"' with value [" + attributeValue + "] to model");
148 					}
149 					model.put(attribute, attributeValue);
150 				}
151 			}
152 		}
153 
154 		if (this.exposeSpringMacroHelpers) {
155 			if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) {
156 				throw new ServletException(
157 						"Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE +
158 						"' because of an existing model object of the same name");
159 			}
160 			// Expose RequestContext instance for Spring macros.
161 			model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE,
162 					new RequestContext(request, response, getServletContext(), model));
163 		}
164 
165 		applyContentType(response);
166 
167 		renderMergedTemplateModel(model, request, response);
168 	}
169 
170 	/**
171 	 * Apply this view's content type as specified in the "contentType"
172 	 * bean property to the given response.
173 	 * <p>Only applies the view's contentType if no content type has been
174 	 * set on the response before. This allows handlers to override the
175 	 * default content type beforehand.
176 	 * @param response current HTTP response
177 	 * @see #setContentType
178 	 */
179 	protected void applyContentType(HttpServletResponse response)	{
180 		if (response.getContentType() == null) {
181 			response.setContentType(getContentType());
182 		}
183 	}
184 
185 	/**
186 	 * Subclasses must implement this method to actually render the view.
187 	 * @param model combined output Map, with request attributes and
188 	 * session attributes merged into it if required
189 	 * @param request current HTTP request
190 	 * @param response current HTTP response
191 	 * @throws Exception if rendering failed
192 	 */
193 	protected abstract void renderMergedTemplateModel(
194 			Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception;
195 
196 }