Update ScriptTemplateView to manage content type

This commit introduces the following changes:
 - Content type can now be properly configured
 - Default content type is "text/html"
 - Content type and charset are now properly set in the response

Issue: SPR-13379
This commit is contained in:
Sebastien Deleuze
2015-08-24 14:27:17 +02:00
parent 88405be8a5
commit 04cff89eb7
9 changed files with 143 additions and 20 deletions

View File

@@ -67,6 +67,9 @@ public class ScriptTemplateConfigurerBeanDefinitionParser extends AbstractSimple
if (element.hasAttribute("render-function")) {
builder.addPropertyValue("renderFunction", element.getAttribute("render-function"));
}
if (element.hasAttribute("content-type")) {
builder.addPropertyValue("contentType", element.getAttribute("content-type"));
}
if (element.hasAttribute("charset")) {
builder.addPropertyValue("charset", Charset.forName(element.getAttribute("charset")));
}
@@ -81,7 +84,8 @@ public class ScriptTemplateConfigurerBeanDefinitionParser extends AbstractSimple
@Override
protected boolean isEligibleAttribute(String name) {
return (name.equals("engine-name") || name.equals("scripts") || name.equals("render-object") ||
name.equals("render-function") || name.equals("charset") || name.equals("resource-loader-path"));
name.equals("render-function") || name.equals("content-type") ||
name.equals("charset") || name.equals("resource-loader-path"));
}
}

View File

@@ -60,6 +60,12 @@ public interface ScriptTemplateConfig {
*/
String getRenderFunction();
/**
* Return the content type to use for the response.
* @since 4.2.1
*/
String getContentType();
/**
* Return the charset used to read script and template files.
*/

View File

@@ -59,6 +59,8 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
private String renderFunction;
private String contentType;
private Charset charset;
private String resourceLoaderPath;
@@ -170,6 +172,24 @@ public class ScriptTemplateConfigurer implements ScriptTemplateConfig {
return this.renderFunction;
}
/**
* Set the content type to use for the response.
* ({@code text/html} by default).
* @since 4.2.1
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* Return the content type to use for the response.
* @since 4.2.1
*/
@Override
public String getContentType() {
return this.contentType;
}
/**
* Set the charset used to read script and template files.
* ({@code UTF-8} by default).

View File

@@ -63,6 +63,8 @@ import org.springframework.web.servlet.view.AbstractUrlBasedView;
*/
public class ScriptTemplateView extends AbstractUrlBasedView {
public static final String DEFAULT_CONTENT_TYPE = "text/html";
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final String DEFAULT_RESOURCE_LOADER_PATH = "classpath:";
@@ -89,6 +91,24 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
private String resourceLoaderPath;
/**
* Constructor for use as a bean.
* @see #setUrl
*/
public ScriptTemplateView() {
setContentType(null);
}
/**
* Create a new ScriptTemplateView with the given URL.
* @since 4.2.1
*/
public ScriptTemplateView(String url) {
super(url);
setContentType(null);
}
/**
* See {@link ScriptTemplateConfigurer#setEngine(ScriptEngine)} documentation.
*/
@@ -132,6 +152,15 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
this.renderFunction = functionName;
}
/**
* See {@link ScriptTemplateConfigurer#setContentType(String)}} documentation.
* @since 4.2.1
*/
@Override
public void setContentType(String contentType) {
super.setContentType(contentType);
}
/**
* See {@link ScriptTemplateConfigurer#setCharset(Charset)} documentation.
*/
@@ -167,6 +196,9 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
if (this.renderFunction == null && viewConfig.getRenderFunction() != null) {
this.renderFunction = viewConfig.getRenderFunction();
}
if (this.getContentType() == null) {
setContentType(viewConfig.getContentType() != null ? viewConfig.getContentType() : DEFAULT_CONTENT_TYPE);
}
if (this.charset == null) {
this.charset = (viewConfig.getCharset() != null ? viewConfig.getCharset() : DEFAULT_CHARSET);
}
@@ -276,10 +308,17 @@ public class ScriptTemplateView extends AbstractUrlBasedView {
}
}
@Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
super.prepareResponse(request, response);
setResponseContentType(request, response);
response.setCharacterEncoding(this.charset.name());
}
@Override
protected void renderMergedOutputModel(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
ScriptEngine engine = getEngine();