KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > AbstractTemplateView


1 /*
2  * Copyright 2002-2006 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 JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.ServletResponse JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27
28 import org.springframework.web.servlet.support.RequestContext;
29
30 /**
31  * AbstractTemplateView provides template based view technologies such as
32  * Velocity and FreeMarker, with the ability to use request and session
33  * attributes in their model and the option to expose helper objects
34  * for Spring's Velocity/FreeMarker macro library.
35  *
36  * <p>JSP/JSTL and other view technologies automatically have access to the
37  * HttpServletRequest object and thereby the request/session attributes
38  * for the current user. Furthermore, they are able to create and cache
39  * helper objects as request attributes themselves.
40  *
41  * @author Darren Davison
42  * @author Juergen Hoeller
43  * @since 1.0.2
44  */

45 public abstract class AbstractTemplateView extends AbstractUrlBasedView {
46
47     /**
48      * Variable name of the RequestContext instance in the template model,
49      * available to Spring's macros: e.g. for creating BindStatus objects.
50      */

51     public static final String JavaDoc SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE = "springMacroRequestContext";
52
53
54     private static boolean responseGetContentTypeAvailable;
55
56     static {
57         // Determine whether the Servlet 2.4 ServletResponse.getContentType method
58
// is available.
59
try {
60             ServletResponse JavaDoc.class.getMethod("getContentType", new Class JavaDoc[] {});
61             responseGetContentTypeAvailable = true;
62         }
63         catch (NoSuchMethodException JavaDoc ex) {
64             responseGetContentTypeAvailable = false;
65         }
66     }
67
68     private boolean exposeRequestAttributes = false;
69
70     private boolean exposeSessionAttributes = false;
71
72     private boolean exposeSpringMacroHelpers = false;
73     
74     private boolean allowRequestOverride = false;
75     
76     private boolean allowSessionOverride = false;
77
78
79     /**
80      * Set whether all request attributes should be added to the
81      * model prior to merging with the template. Default is "false".
82      */

83     public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
84         this.exposeRequestAttributes = exposeRequestAttributes;
85     }
86
87     /**
88      * Set whether all HttpSession attributes should be added to the
89      * model prior to merging with the template. Default is "false".
90      */

91     public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
92         this.exposeSessionAttributes = exposeSessionAttributes;
93     }
94
95     /**
96      * Set whether HttpServletRequest attributes are allowed to override (hide)
97      * controller generated model attributes of the same name. Default is "false",
98      * which causes an exception to be thrown if request attributes of the same
99      * name as model attributes are found.
100      */

101     public void setAllowRequestOverride(boolean allowRequestOverride) {
102         this.allowRequestOverride = allowRequestOverride;
103     }
104     
105     /**
106      * Set whether HttpSession attributes are allowed to override (hide)
107      * controller generated model attributes of the same name. Default is "false",
108      * which causes an exception to be thrown if session attributes of the same
109      * name as model attributes are found.
110      */

111     public void setAllowSessionOverride(boolean allowSessionOverride) {
112         this.allowSessionOverride = allowSessionOverride;
113     }
114
115     /**
116      * Set whether to expose a RequestContext for use by Spring's macro library,
117      * under the name "springMacroRequestContext". Default is "false".
118      * <p>Currently needed for Spring's Velocity and FreeMarker default macros.
119      * Note that this is <b>not</b> required for templates that use HTML
120      * forms <b>unless</b> you wish to take advantage of the Spring helper macros.
121      * @see #SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
122      */

123     public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
124         this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
125     }
126
127
128     protected final void renderMergedOutputModel(
129             Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
130
131         if (this.exposeRequestAttributes) {
132             for (Enumeration JavaDoc en = request.getAttributeNames(); en.hasMoreElements();) {
133                 String JavaDoc attribute = (String JavaDoc) en.nextElement();
134                 if (model.containsKey(attribute) && !this.allowRequestOverride) {
135                     throw new ServletException JavaDoc("Cannot expose request attribute '" + attribute +
136                         "' because of an existing model object of the same name");
137                 }
138                 Object JavaDoc attributeValue = request.getAttribute(attribute);
139                 if (logger.isDebugEnabled()) {
140                     logger.debug("Exposing request attribute '" + attribute +
141                             "' with value [" + attributeValue + "] to model");
142                 }
143                 model.put(attribute, attributeValue);
144             }
145         }
146
147         if (this.exposeSessionAttributes) {
148             HttpSession JavaDoc session = request.getSession(false);
149             if (session != null) {
150                 for (Enumeration JavaDoc en = session.getAttributeNames(); en.hasMoreElements();) {
151                     String JavaDoc attribute = (String JavaDoc) en.nextElement();
152                     if (model.containsKey(attribute) && !this.allowSessionOverride) {
153                         throw new ServletException JavaDoc("Cannot expose session attribute '" + attribute +
154                             "' because of an existing model object of the same name");
155                     }
156                     Object JavaDoc attributeValue = session.getAttribute(attribute);
157                     if (logger.isDebugEnabled()) {
158                         logger.debug("Exposing session attribute '" + attribute +
159                                 "' with value [" + attributeValue + "] to model");
160                     }
161                     model.put(attribute, attributeValue);
162                 }
163             }
164         }
165
166         if (this.exposeSpringMacroHelpers) {
167             if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) {
168                 throw new ServletException JavaDoc(
169                         "Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE +
170                         "' because of an existing model object of the same name");
171             }
172             // expose RequestContext instance for Spring macros
173
model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, new RequestContext(request, model));
174         }
175
176         applyContentType(response);
177
178         renderMergedTemplateModel(model, request, response);
179     }
180
181     /**
182      * Apply this view's content type as specified in the "contentType"
183      * bean property to the given response.
184      * <p>When running on Servlet 2.4, only applies the view's contentType
185      * if no content type has been set on the response before. This allows
186      * handlers to override the default content type beforehand.
187      * @param response current HTTP response
188      * @see #setContentType
189      */

190     protected void applyContentType(HttpServletResponse JavaDoc response) {
191         if (!responseGetContentTypeAvailable || response.getContentType() == null) {
192             response.setContentType(getContentType());
193         }
194     }
195
196     /**
197      * Subclasses must implement this method to actually render the view.
198      * @param model combined output Map, with request attributes and
199      * session attributes merged into it if required
200      * @param request current HTTP request
201      * @param response current HTTP response
202      * @throws Exception if rendering failed
203      */

204     protected abstract void renderMergedTemplateModel(
205             Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc;
206
207 }
208
Popular Tags