commit
3137f20b4a
|
@ -18,7 +18,10 @@ package org.springframework.boot.autoconfigure.mustache;
|
|||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.http.MediaType;
|
||||
|
@ -41,7 +44,7 @@ public class MustacheProperties {
|
|||
|
||||
public static final String DEFAULT_SUFFIX = ".mustache";
|
||||
|
||||
private final Servlet servlet = new Servlet();
|
||||
private final Servlet servlet = new Servlet(this::getCharset);
|
||||
|
||||
private final Reactive reactive = new Reactive();
|
||||
|
||||
|
@ -190,6 +193,16 @@ public class MustacheProperties {
|
|||
*/
|
||||
private boolean exposeSpringMacroHelpers = true;
|
||||
|
||||
private final Supplier<Charset> charset;
|
||||
|
||||
public Servlet() {
|
||||
this.charset = () -> null;
|
||||
}
|
||||
|
||||
private Servlet(Supplier<Charset> charset) {
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
public boolean isAllowRequestOverride() {
|
||||
return this.allowRequestOverride;
|
||||
}
|
||||
|
@ -215,6 +228,15 @@ public class MustacheProperties {
|
|||
}
|
||||
|
||||
public MimeType getContentType() {
|
||||
if (this.contentType != null && this.contentType.getCharset() == null) {
|
||||
Charset charset = this.charset.get();
|
||||
if (charset != null) {
|
||||
Map<String, String> parameters = new LinkedHashMap<>();
|
||||
parameters.put("charset", charset.name());
|
||||
parameters.putAll(this.contentType.getParameters());
|
||||
return new MimeType(this.contentType, parameters);
|
||||
}
|
||||
}
|
||||
return this.contentType;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -117,6 +117,7 @@ class MustacheAutoConfigurationTests {
|
|||
assertThat(viewResolver).extracting("allowSessionOverride", InstanceOfAssertFactories.BOOLEAN).isFalse();
|
||||
assertThat(viewResolver).extracting("cache", InstanceOfAssertFactories.BOOLEAN).isFalse();
|
||||
assertThat(viewResolver).extracting("charset").isEqualTo("UTF-8");
|
||||
assertThat(viewResolver).extracting("contentType").isEqualTo("text/html;charset=UTF-8");
|
||||
assertThat(viewResolver).extracting("exposeRequestAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
|
||||
assertThat(viewResolver).extracting("exposeSessionAttributes", InstanceOfAssertFactories.BOOLEAN).isFalse();
|
||||
assertThat(viewResolver).extracting("exposeSpringMacroHelpers", InstanceOfAssertFactories.BOOLEAN).isTrue();
|
||||
|
@ -161,6 +162,10 @@ class MustacheAutoConfigurationTests {
|
|||
@EnumSource
|
||||
void charsetCanBeCustomizedOnViewResolver(ViewResolverKind kind) {
|
||||
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "charset", "UTF-16");
|
||||
if (kind == ViewResolverKind.SERVLET) {
|
||||
assertViewResolverProperty(kind, "spring.mustache.charset=UTF-16", "contentType",
|
||||
"text/html;charset=UTF-16");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue