Skip to content

Commit 8ec10c8

Browse files
committed
Use buildView() to create MustacheView
Update MustacheViewResolver so that buildView() is called to create the MustacheView. This sets fields such as `contentType` and allows us to remove explicit setApplicationContext() and setServletContext() calls. Fixes gh-3265
1 parent 1ecc9c8 commit 8ec10c8

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheView.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,44 @@
3030
* Spring MVC {@link View} using the Mustache template engine.
3131
*
3232
* @author Dave Syer
33+
* @author Phillip Webb
3334
* @since 1.2.2
3435
*/
3536
public class MustacheView extends AbstractTemplateView {
3637

37-
private final Template template;
38+
private Template template;
3839

40+
/**
41+
* Create a new {@link MustacheView} instance.
42+
* @see #setTemplate(Template)
43+
* @since 1.2.5
44+
*/
45+
public MustacheView() {
46+
}
47+
48+
/**
49+
* Create a new {@link MustacheView} with the specified template.
50+
* @param template the source template
51+
*/
3952
public MustacheView(Template template) {
4053
this.template = template;
4154
}
4255

56+
/**
57+
* Set the Mustache template that should actually be rendered.
58+
* @param template the mustache template
59+
* @since 1.2.5
60+
*/
61+
public void setTemplate(Template template) {
62+
this.template = template;
63+
}
64+
4365
@Override
4466
protected void renderMergedTemplateModel(Map<String, Object> model,
4567
HttpServletRequest request, HttpServletResponse response) throws Exception {
46-
this.template.execute(model, response.getWriter());
68+
if (this.template != null) {
69+
this.template.execute(model, response.getWriter());
70+
}
4771
}
4872

4973
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolver.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStreamReader;
21+
import java.io.Reader;
2122
import java.util.Locale;
2223

2324
import org.springframework.beans.propertyeditors.LocaleEditor;
@@ -35,6 +36,7 @@
3536
*
3637
* @author Dave Syer
3738
* @author Andy Wilkinson
39+
* @author Phillip Webb
3840
* @since 1.2.2
3941
*/
4042
public class MustacheViewResolver extends UrlBasedViewResolver {
@@ -44,7 +46,12 @@ public class MustacheViewResolver extends UrlBasedViewResolver {
4446
private String charset;
4547

4648
public MustacheViewResolver() {
47-
setViewClass(MustacheView.class);
49+
setViewClass(requiredViewClass());
50+
}
51+
52+
@Override
53+
protected Class<?> requiredViewClass() {
54+
return MustacheView.class;
4855
}
4956

5057
/**
@@ -67,31 +74,15 @@ protected View loadView(String viewName, Locale locale) throws Exception {
6774
if (resource == null) {
6875
return null;
6976
}
70-
MustacheView view = new MustacheView(createTemplate(resource));
71-
view.setApplicationContext(getApplicationContext());
72-
view.setServletContext(getServletContext());
73-
return view;
74-
}
75-
76-
private Template createTemplate(Resource resource) throws IOException {
77-
return this.charset == null ? this.compiler.compile(new InputStreamReader(
78-
resource.getInputStream())) : this.compiler
79-
.compile(new InputStreamReader(resource.getInputStream(), this.charset));
77+
MustacheView mustacheView = (MustacheView) super.loadView(viewName, locale);
78+
mustacheView.setTemplate(createTemplate(resource));
79+
return mustacheView;
8080
}
8181

8282
private Resource resolveResource(String viewName, Locale locale) {
8383
return resolveFromLocale(viewName, getLocale(locale));
8484
}
8585

86-
private String getLocale(Locale locale) {
87-
if (locale == null) {
88-
return "";
89-
}
90-
LocaleEditor localeEditor = new LocaleEditor();
91-
localeEditor.setValue(locale);
92-
return "_" + localeEditor.getAsText();
93-
}
94-
9586
private Resource resolveFromLocale(String viewName, String locale) {
9687
Resource resource = getApplicationContext().getResource(
9788
getPrefix() + viewName + locale + getSuffix());
@@ -105,4 +96,24 @@ private Resource resolveFromLocale(String viewName, String locale) {
10596
return resource;
10697
}
10798

99+
private String getLocale(Locale locale) {
100+
if (locale == null) {
101+
return "";
102+
}
103+
LocaleEditor localeEditor = new LocaleEditor();
104+
localeEditor.setValue(locale);
105+
return "_" + localeEditor.getAsText();
106+
}
107+
108+
private Template createTemplate(Resource resource) throws IOException {
109+
return this.compiler.compile(getReader(resource));
110+
}
111+
112+
private Reader getReader(Resource resource) throws IOException {
113+
if (this.charset != null) {
114+
return new InputStreamReader(resource.getInputStream(), this.charset);
115+
}
116+
return new InputStreamReader(resource.getInputStream());
117+
}
118+
108119
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mustache/web/MustacheViewResolverTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
import org.junit.Before;
2222
import org.junit.Test;
23-
import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver;
2423
import org.springframework.mock.web.MockServletContext;
2524
import org.springframework.web.context.support.StaticWebApplicationContext;
25+
import org.springframework.web.servlet.View;
2626

27+
import static org.hamcrest.Matchers.equalTo;
2728
import static org.junit.Assert.assertNotNull;
2829
import static org.junit.Assert.assertNull;
30+
import static org.junit.Assert.assertThat;
2931

3032
/**
3133
* Tests for {@link MustacheViewResolver}.
@@ -74,4 +76,12 @@ public void resolveSpecificLocale() throws Exception {
7476
assertNotNull(this.resolver.resolveViewName("foo", new Locale("de")));
7577
}
7678

79+
@Test
80+
public void setsContentType() throws Exception {
81+
this.resolver.setContentType("application/octet-stream");
82+
View view = this.resolver.resolveViewName("foo", null);
83+
assertThat(view.getContentType(), equalTo("application/octet-stream"));
84+
85+
}
86+
7787
}

0 commit comments

Comments
 (0)