1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class AbstractTemplateView extends AbstractUrlBasedView {
47
48
49
50
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
68
69
70 public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
71 this.exposeRequestAttributes = exposeRequestAttributes;
72 }
73
74
75
76
77
78
79
80 public void setAllowRequestOverride(boolean allowRequestOverride) {
81 this.allowRequestOverride = allowRequestOverride;
82 }
83
84
85
86
87
88 public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
89 this.exposeSessionAttributes = exposeSessionAttributes;
90 }
91
92
93
94
95
96
97
98 public void setAllowSessionOverride(boolean allowSessionOverride) {
99 this.allowSessionOverride = allowSessionOverride;
100 }
101
102
103
104
105
106
107
108
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
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
172
173
174
175
176
177
178
179 protected void applyContentType(HttpServletResponse response) {
180 if (response.getContentType() == null) {
181 response.setContentType(getContentType());
182 }
183 }
184
185
186
187
188
189
190
191
192
193 protected abstract void renderMergedTemplateModel(
194 Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception;
195
196 }