1
1
/*
2
- * Copyright 2002-2009 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import java .util .Map ;
24
24
import java .util .Properties ;
25
25
import java .util .StringTokenizer ;
26
+
26
27
import javax .servlet .ServletOutputStream ;
27
28
import javax .servlet .http .HttpServletRequest ;
28
29
import javax .servlet .http .HttpServletResponse ;
29
30
30
31
import org .springframework .beans .factory .BeanNameAware ;
32
+ import org .springframework .http .MediaType ;
31
33
import org .springframework .util .CollectionUtils ;
32
34
import org .springframework .web .context .support .WebApplicationObjectSupport ;
33
35
import org .springframework .web .servlet .View ;
@@ -223,15 +225,15 @@ public Map<String, Object> getStaticAttributes() {
223
225
}
224
226
225
227
/**
226
- * Whether to add path variables in the model or not.
227
- * <p>Path variables are commonly bound to URI template variables through the {@code @PathVariable}
228
- * annotation. They're are effectively URI template variables with type conversion applied to
229
- * them to derive typed Object values. Such values are frequently needed in views for
230
- * constructing links to the same and other URLs.
231
- * <p>Path variables added to the model override static attributes (see {@link #setAttributes(Properties)})
232
- * but not attributes already present in the model.
233
- * <p>By default this flag is set to {@code true}. Concrete view types can override this.
234
- * @param exposePathVariables {@code true} to expose path variables, and {@code false} otherwise.
228
+ * Whether to add path variables in the model or not.
229
+ * <p>Path variables are commonly bound to URI template variables through the {@code @PathVariable}
230
+ * annotation. They're are effectively URI template variables with type conversion applied to
231
+ * them to derive typed Object values. Such values are frequently needed in views for
232
+ * constructing links to the same and other URLs.
233
+ * <p>Path variables added to the model override static attributes (see {@link #setAttributes(Properties)})
234
+ * but not attributes already present in the model.
235
+ * <p>By default this flag is set to {@code true}. Concrete view types can override this.
236
+ * @param exposePathVariables {@code true} to expose path variables, and {@code false} otherwise.
235
237
*/
236
238
public void setExposePathVariables (boolean exposePathVariables ) {
237
239
this .exposePathVariables = exposePathVariables ;
@@ -255,15 +257,15 @@ public void render(Map<String, ?> model, HttpServletRequest request, HttpServlet
255
257
logger .trace ("Rendering view with name '" + this .beanName + "' with model " + model +
256
258
" and static attributes " + this .staticAttributes );
257
259
}
258
-
260
+
259
261
Map <String , Object > mergedModel = createMergedOutputModel (model , request , response );
260
262
261
263
prepareResponse (request , response );
262
264
renderMergedOutputModel (mergedModel , request , response );
263
265
}
264
266
265
267
/**
266
- * Creates a combined output Map (never <code>null</code>) that includes dynamic values and static attributes.
268
+ * Creates a combined output Map (never <code>null</code>) that includes dynamic values and static attributes.
267
269
* Dynamic values take precedence over static attributes.
268
270
*/
269
271
protected Map <String , Object > createMergedOutputModel (Map <String , ?> model , HttpServletRequest request ,
@@ -289,7 +291,7 @@ protected Map<String, Object> createMergedOutputModel(Map<String, ?> model, Http
289
291
if (this .requestContextAttribute != null ) {
290
292
mergedModel .put (this .requestContextAttribute , createRequestContext (request , response , mergedModel ));
291
293
}
292
-
294
+
293
295
return mergedModel ;
294
296
}
295
297
@@ -408,6 +410,21 @@ protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStre
408
410
out .flush ();
409
411
}
410
412
413
+ /**
414
+ * Set the content type of the response to the configured
415
+ * {@link #setContentType(String) content type} unless the
416
+ * {@link View#SELECTED_CONTENT_TYPE} request attribute is present and set
417
+ * to a concrete media type.
418
+ */
419
+ protected void setResponseContentType (HttpServletRequest request , HttpServletResponse response ) {
420
+ MediaType mediaType = (MediaType ) request .getAttribute (View .SELECTED_CONTENT_TYPE );
421
+ if (mediaType != null && mediaType .isConcrete ()) {
422
+ response .setContentType (mediaType .toString ());
423
+ }
424
+ else {
425
+ response .setContentType (getContentType ());
426
+ }
427
+ }
411
428
412
429
@ Override
413
430
public String toString () {
0 commit comments