Merge branch '6.1.x'

This commit is contained in:
Sam Brannen
2024-06-25 16:58:45 +02:00
18 changed files with 692 additions and 290 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 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.
@@ -20,8 +20,9 @@ import freemarker.template.Configuration;
/**
* Interface to be implemented by objects that configure and manage a
* FreeMarker Configuration object in a web environment. Detected and
* used by {@link FreeMarkerView}.
* FreeMarker {@link Configuration} object in a web environment.
*
* <p>Detected and used by {@link FreeMarkerView}.
*
* @author Darren Davison
* @author Rob Harrop
@@ -34,9 +35,9 @@ public interface FreeMarkerConfig {
/**
* Return the FreeMarker {@link Configuration} object for the current
* web application context.
* <p>A FreeMarker Configuration object may be used to set FreeMarker
* <p>A FreeMarker {@code Configuration} object may be used to set FreeMarker
* properties and shared objects, and allows to retrieve templates.
* @return the FreeMarker Configuration
* @return the FreeMarker {@code Configuration}
*/
Configuration getConfiguration();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 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.
@@ -31,9 +31,10 @@ import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.util.Assert;
/**
* JavaBean to configure FreeMarker for web usage, via the "configLocation"
* and/or "freemarkerSettings" and/or "templateLoaderPath" properties.
* The simplest way to use this class is to specify just a "templateLoaderPath";
* Bean to configure FreeMarker for web usage, via the "configLocation",
* "freemarkerSettings", or "templateLoaderPath" properties.
*
* <p>The simplest way to use this class is to specify just a "templateLoaderPath";
* you do not need any further configuration then.
*
* <pre class="code">
@@ -41,17 +42,17 @@ import org.springframework.util.Assert;
* &lt;property name="templateLoaderPath"&gt;&lt;value&gt;/WEB-INF/freemarker/&lt;/value&gt;&lt;/property&gt;
* &lt;/bean&gt;</pre>
*
* This bean must be included in the application context of any application
* using Spring's FreeMarkerView for web MVC. It exists purely to configure FreeMarker.
* It is not meant to be referenced by application components but just internally
* by FreeMarkerView. Implements FreeMarkerConfig to be found by FreeMarkerView without
* depending on the bean name of the configurer. Each DispatcherServlet can define its
* own FreeMarkerConfigurer if desired.
* <p>This bean must be included in the application context of any application
* using Spring's {@link FreeMarkerView} for web MVC. It exists purely to configure
* FreeMarker. It is not meant to be referenced by application components but just
* internally by {@code FreeMarkerView}. Implements {@link FreeMarkerConfig} to
* be found by {@code FreeMarkerView} without depending on the bean name of the
* configurer. Each DispatcherServlet can define its own {@code FreeMarkerConfigurer}
* if desired.
*
* <p>Note that you can also refer to a preconfigured FreeMarker Configuration
* instance, for example one set up by FreeMarkerConfigurationFactoryBean, via
* the "configuration" property. This allows to share a FreeMarker Configuration
* for web and email usage, for example.
* <p>Note that you can also refer to a pre-configured FreeMarker {@code Configuration}
* instance via the "configuration" property. This allows to share a FreeMarker
* {@code Configuration} for web and email usage for example.
*
* <p>This configurer registers a template loader for this package, allowing to
* reference the "spring.ftl" macro library contained in this package:
@@ -61,7 +62,7 @@ import org.springframework.util.Assert;
* &lt;@spring.bind "person.age"/&gt;
* age is ${spring.status.value}</pre>
*
* Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
*
* @author Darren Davison
* @author Rob Harrop
@@ -81,10 +82,10 @@ public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
/**
* Set a preconfigured Configuration to use for the FreeMarker web config, e.g. a
* shared one for web and email usage, set up via FreeMarkerConfigurationFactoryBean.
* If this is not set, FreeMarkerConfigurationFactory's properties (inherited by
* this class) have to be specified.
* Set a preconfigured {@link Configuration} to use for the FreeMarker web
* config &mdash; for example, a shared one for web and email usage.
* <p>If this is not set, FreeMarkerConfigurationFactory's properties (inherited
* by this class) have to be specified.
* @see org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean
*/
public void setConfiguration(Configuration configuration) {
@@ -93,11 +94,13 @@ public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
/**
* Initialize FreeMarkerConfigurationFactory's Configuration
* if not overridden by a preconfigured FreeMarker Configuration.
* <p>Sets up a ClassTemplateLoader to use for loading Spring macros.
* Initialize FreeMarkerConfigurationFactory's {@link Configuration}
* if not overridden by a preconfigured FreeMarker {@code Configuration}.
* <p>Indirectly sets up a {@link ClassTemplateLoader} to use for loading
* Spring macros.
* @see #createConfiguration
* @see #setConfiguration
* @see #postProcessTemplateLoaders(List)
*/
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
@@ -107,7 +110,7 @@ public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
}
/**
* This implementation registers an additional ClassTemplateLoader
* This implementation registers an additional {@link ClassTemplateLoader}
* for the Spring-provided macros, added to the end of the list.
*/
@Override
@@ -117,7 +120,7 @@ public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
/**
* Return the Configuration object wrapped by this bean.
* Return the {@link Configuration} object wrapped by this bean.
*/
@Override
public Configuration getConfiguration() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@@ -46,27 +46,40 @@ import org.springframework.web.servlet.view.AbstractTemplateView;
/**
* View using the FreeMarker template engine.
*
* <p>Exposes the following JavaBean properties:
* <p>Exposes the following configuration properties:
* <ul>
* <li><b>url</b>: the location of the FreeMarker template to be wrapped,
* relative to the FreeMarker template context (directory).
* <li><b>encoding</b> (optional, default is determined by FreeMarker configuration):
* the encoding of the FreeMarker template file
* <li><b>{@link #setUrl(String) url}</b>: the location of the FreeMarker template
* relative to the FreeMarker template context (directory).</li>
* <li><b>{@link #setEncoding(String) encoding}</b>: the encoding used to decode
* byte sequences to character sequences when reading the FreeMarker template file.
* Default is determined by the FreeMarker {@link Configuration}.</li>
* <li><b>{@link #setContentType(String) contentType}</b>: the content type of the
* rendered response. Defaults to {@code "text/html;charset=ISO-8859-1"} but should
* typically be set to a value that corresponds to the actual generated content
* type (see note below).</li>
* </ul>
*
* <p>Depends on a single {@link FreeMarkerConfig} object such as {@link FreeMarkerConfigurer}
* being accessible in the current web application context, with any bean name.
* Alternatively, you can set the FreeMarker {@link Configuration} object as a
* bean property. See {@link #setConfiguration} for more details on the impacts
* of this approach.
* <p>Depends on a single {@link FreeMarkerConfig} object such as
* {@link FreeMarkerConfigurer} being accessible in the current web application
* context. Alternatively the FreeMarker {@link Configuration} can be set directly
* via {@link #setConfiguration}.
*
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
* <p><b>Note:</b> To ensure that the correct encoding is used when rendering the
* response, set the {@linkplain #setContentType(String) content type} with an
* appropriate {@code charset} attribute &mdash; for example,
* {@code "text/html;charset=UTF-8"}. When using {@link FreeMarkerViewResolver}
* to create the view for you, set the
* {@linkplain FreeMarkerViewResolver#setContentType(String) content type}
* directly in the {@code FreeMarkerViewResolver}.
*
* <p>Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.
* As of Spring Framework 6.0, FreeMarker templates are rendered in a minimal
* fashion without JSP support, just exposing request attributes in addition
* to the MVC-provided model map for alignment with common Servlet resources.
*
* @author Darren Davison
* @author Juergen Hoeller
* @author Sam Brannen
* @since 03.03.2004
* @see #setUrl
* @see #setExposeSpringMacroHelpers
@@ -85,17 +98,36 @@ public class FreeMarkerView extends AbstractTemplateView {
/**
* Set the encoding of the FreeMarker template file. Default is determined
* by the FreeMarker Configuration: "ISO-8859-1" if not specified otherwise.
* <p>Specify the encoding in the FreeMarker Configuration rather than per
* template if all your templates share a common encoding.
* Set the encoding used to decode byte sequences to character sequences when
* reading the FreeMarker template file for this view.
* <p>Defaults to {@code null} to signal that the FreeMarker
* {@link Configuration} should be used to determine the encoding.
* <p>A non-null encoding will override the default encoding determined by
* the FreeMarker {@code Configuration}.
* <p>If the encoding is not explicitly set here or in the FreeMarker
* {@code Configuration}, FreeMarker will read template files using the platform
* file encoding (defined by the JVM system property {@code file.encoding})
* or {@code "utf-8"} if the platform file encoding is undefined.
* <p>It's recommended to specify the encoding in the FreeMarker {@code Configuration}
* rather than per template if all your templates share a common encoding.
* <p>Note that the specified or default encoding is not used for template
* rendering. Instead, an explicit encoding must be specified for the rendering
* process. See the note in the {@linkplain FreeMarkerView class-level
* documentation} for details.
* @see freemarker.template.Configuration#setDefaultEncoding
* @see #getEncoding()
* @see #setContentType(String)
*/
public void setEncoding(@Nullable String encoding) {
this.encoding = encoding;
}
/**
* Return the encoding for the FreeMarker template.
* Get the encoding used to decode byte sequences to character sequences
* when reading the FreeMarker template file for this view, or {@code null}
* to signal that the FreeMarker {@link Configuration} should be used to
* determine the encoding.
* @see #setEncoding(String)
*/
@Nullable
protected String getEncoding() {
@@ -103,8 +135,8 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Set the FreeMarker Configuration to be used by this view.
* <p>If this is not set, the default lookup will occur: a single {@link FreeMarkerConfig}
* Set the FreeMarker {@link Configuration} to be used by this view.
* <p>If not set, the default lookup will occur: a single {@link FreeMarkerConfig}
* is expected in the current web application context, with any bean name.
*/
public void setConfiguration(@Nullable Configuration configuration) {
@@ -112,7 +144,7 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Return the FreeMarker configuration used by this view.
* Return the FreeMarker {@link Configuration} used by this view.
*/
@Nullable
protected Configuration getConfiguration() {
@@ -120,7 +152,7 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Obtain the FreeMarker configuration for actual use.
* Obtain the FreeMarker {@link Configuration} for actual use.
* @return the FreeMarker configuration (never {@code null})
* @throws IllegalStateException in case of no Configuration object set
* @since 5.0
@@ -133,8 +165,8 @@ public class FreeMarkerView extends AbstractTemplateView {
/**
* Invoked on startup. Looks for a single FreeMarkerConfig bean to
* find the relevant Configuration for this factory.
* Invoked on startup. Looks for a single {@link FreeMarkerConfig} bean to
* find the relevant {@link Configuration} for this view.
* <p>Checks that the template for the default Locale can be found:
* FreeMarker will check non-Locale-specific templates if a
* locale-specific one is not found.
@@ -149,9 +181,9 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Autodetect a {@link FreeMarkerConfig} object via the ApplicationContext.
* @return the Configuration instance to use for FreeMarkerViews
* @throws BeansException if no Configuration instance could be found
* Autodetect a {@link FreeMarkerConfig} object via the {@code ApplicationContext}.
* @return the {@code FreeMarkerConfig} instance to use for FreeMarkerViews
* @throws BeansException if no {@link FreeMarkerConfig} bean could be found
* @see #getApplicationContext
* @see #setConfiguration
*/
@@ -170,7 +202,7 @@ public class FreeMarkerView extends AbstractTemplateView {
/**
* Return the configured FreeMarker {@link ObjectWrapper}, or the
* {@link ObjectWrapper#DEFAULT_WRAPPER default wrapper} if none specified.
* {@linkplain ObjectWrapper#DEFAULT_WRAPPER default wrapper} if none specified.
* @see freemarker.template.Configuration#getObjectWrapper()
*/
protected ObjectWrapper getObjectWrapper() {
@@ -209,7 +241,7 @@ public class FreeMarkerView extends AbstractTemplateView {
/**
* Process the model map by merging it with the FreeMarker template.
* Output is directed to the servlet response.
* <p>Output is directed to the servlet response.
* <p>This method can be overridden if custom behavior is needed.
*/
@Override
@@ -284,12 +316,12 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Retrieve the FreeMarker template for the given locale,
* to be rendering by this view.
* Retrieve the FreeMarker {@link Template} for the given locale, to be
* rendered by this view.
* <p>By default, the template specified by the "url" bean property
* will be retrieved.
* @param locale the current locale
* @return the FreeMarker template to render
* @return the FreeMarker {@code Template} to render
* @throws IOException if the template file could not be retrieved
* @see #setUrl
* @see #getTemplate(String, java.util.Locale)
@@ -301,14 +333,15 @@ public class FreeMarkerView extends AbstractTemplateView {
}
/**
* Retrieve the FreeMarker template specified by the given name,
* using the encoding specified by the "encoding" bean property.
* Retrieve the FreeMarker {@link Template} for the specified name and locale,
* using the {@linkplain #setEncoding(String) configured encoding} if set.
* <p>Can be called by subclasses to retrieve a specific template,
* for example to render multiple templates into a single view.
* @param name the file name of the desired template
* @param locale the current locale
* @return the FreeMarker template
* @throws IOException if the template file could not be retrieved
* @see #setEncoding(String)
*/
protected Template getTemplate(String name, Locale locale) throws IOException {
return (getEncoding() != null ?

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2024 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.
@@ -24,17 +24,24 @@ import org.springframework.web.servlet.view.AbstractUrlBasedView;
* that supports {@link FreeMarkerView} (i.e. FreeMarker templates) and custom subclasses of it.
*
* <p>The view class for all views generated by this resolver can be specified
* via the "viewClass" property. See UrlBasedViewResolver's javadoc for details.
* via the "viewClass" property. See {@code UrlBasedViewResolver} for details.
*
* <p><b>Note:</b> When chaining ViewResolvers, a FreeMarkerViewResolver will
* <p><b>Note:</b> To ensure that the correct encoding is used when the rendering
* the response, set the {@linkplain #setContentType(String) content type} with an
* appropriate {@code charset} attribute &mdash; for example,
* {@code "text/html;charset=UTF-8"}.
*
* <p><b>Note:</b> When chaining ViewResolvers, a {@code FreeMarkerViewResolver} will
* check for the existence of the specified template resources and only return
* a non-null View object if the template was actually found.
* a non-null {@code View} object if the template was actually found.
*
* @author Juergen Hoeller
* @author Sam Brannen
* @since 1.1
* @see #setViewClass
* @see #setPrefix
* @see #setSuffix
* @see #setContentType
* @see #setRequestContextAttribute
* @see #setExposeSpringMacroHelpers
* @see FreeMarkerView

View File

@@ -16,9 +16,10 @@
package org.springframework.web.servlet.config.annotation;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
@@ -43,47 +44,197 @@ import static org.assertj.core.api.Assertions.assertThatRuntimeException;
* Integration tests for view resolution with {@code @EnableWebMvc}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.1
*/
class ViewResolutionIntegrationTests {
private static final String EXPECTED_BODY = "<html><body>Hello World!</body></html>";
private static final String EXPECTED_BODY = "<html><body>Hello, Java Café</body></html>";
@Test
void freemarker() throws Exception {
MockHttpServletResponse response = runTest(FreeMarkerWebConfig.class);
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
}
@Test // SPR-12013
void freemarkerWithExistingViewResolver() throws Exception {
MockHttpServletResponse response = runTest(ExistingViewResolverConfig.class);
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
}
@Test
void groovyMarkup() throws Exception {
MockHttpServletResponse response = runTest(GroovyMarkupWebConfig.class);
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
}
@Test
void freemarkerInvalidConfig() {
assertThatRuntimeException()
.isThrownBy(() -> runTest(InvalidFreeMarkerWebConfig.class))
.withMessageContaining("In addition to a FreeMarker view resolver ");
}
@Test
void groovyMarkupInvalidConfig() {
assertThatRuntimeException()
.isThrownBy(() -> runTest(InvalidGroovyMarkupWebConfig.class))
.withMessageContaining("In addition to a Groovy markup view resolver ");
@BeforeAll
static void verifyDefaultFileEncoding() {
assertThat(System.getProperty("file.encoding")).as("JVM default file encoding").isEqualTo("UTF-8");
}
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
@Nested
class FreeMarkerTests {
@Test
void freemarkerWithInvalidConfig() {
assertThatRuntimeException()
.isThrownBy(() -> runTest(InvalidFreeMarkerWebConfig.class))
.withMessageContaining("In addition to a FreeMarker view resolver ");
}
@Test
void freemarkerWithDefaults() throws Exception {
MockHttpServletResponse response = runTest(FreeMarkerWebConfig.class);
assertThat(response.isCharset()).as("character encoding set in response").isTrue();
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
// Prior to Spring Framework 6.2, the charset is not updated in the Content-Type.
// Thus, we expect ISO-8859-1 instead of UTF-8.
assertThat(response.getCharacterEncoding()).isEqualTo("ISO-8859-1");
assertThat(response.getContentType()).isEqualTo("text/html;charset=ISO-8859-1");
}
@Test // gh-16629, gh-33071
void freemarkerWithExistingViewResolver() throws Exception {
MockHttpServletResponse response = runTest(ExistingViewResolverConfig.class);
assertThat(response.isCharset()).as("character encoding set in response").isTrue();
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
// Prior to Spring Framework 6.2, the charset is not updated in the Content-Type.
// Thus, we expect ISO-8859-1 instead of UTF-8.
assertThat(response.getCharacterEncoding()).isEqualTo("ISO-8859-1");
assertThat(response.getContentType()).isEqualTo("text/html;charset=ISO-8859-1");
}
@Test // gh-33071
void freemarkerWithExplicitDefaultEncoding() throws Exception {
MockHttpServletResponse response = runTest(ExplicitDefaultEncodingConfig.class);
assertThat(response.isCharset()).as("character encoding set in response").isTrue();
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
// Prior to Spring Framework 6.2, the charset is not updated in the Content-Type.
// Thus, we expect ISO-8859-1 instead of UTF-8.
assertThat(response.getCharacterEncoding()).isEqualTo("ISO-8859-1");
assertThat(response.getContentType()).isEqualTo("text/html;charset=ISO-8859-1");
}
@Test // gh-33071
void freemarkerWithExplicitDefaultEncodingAndContentType() throws Exception {
MockHttpServletResponse response = runTest(ExplicitDefaultEncodingAndContentTypeConfig.class);
assertThat(response.isCharset()).as("character encoding set in response").isTrue();
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
// When the Content-Type is explicitly set on the view resolver, it should be used.
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-16");
assertThat(response.getContentType()).isEqualTo("text/html;charset=UTF-16");
}
@Configuration
static class InvalidFreeMarkerWebConfig extends WebMvcConfigurationSupport {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.freeMarker();
}
}
@Configuration
static class FreeMarkerWebConfig extends AbstractWebConfig {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.freeMarker();
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
return configurer;
}
}
@Configuration
static class ExistingViewResolverConfig extends AbstractWebConfig {
@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
return new FreeMarkerViewResolver("", ".ftl");
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
return configurer;
}
}
@Configuration
static class ExplicitDefaultEncodingConfig extends AbstractWebConfig {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.freeMarker();
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
configurer.setDefaultEncoding(StandardCharsets.UTF_8.name());
return configurer;
}
}
@Configuration
static class ExplicitDefaultEncodingAndContentTypeConfig extends AbstractWebConfig {
@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver("", ".ftl");
resolver.setContentType("text/html;charset=UTF-16");
return resolver;
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
configurer.setDefaultEncoding(StandardCharsets.UTF_8.name());
return configurer;
}
}
}
@Nested
class GroovyMarkupTests {
@Test
void groovyMarkupInvalidConfig() {
assertThatRuntimeException()
.isThrownBy(() -> runTest(InvalidGroovyMarkupWebConfig.class))
.withMessageContaining("In addition to a Groovy markup view resolver ");
}
@Test
void groovyMarkup() throws Exception {
MockHttpServletResponse response = runTest(GroovyMarkupWebConfig.class);
assertThat(response.getContentAsString()).isEqualTo(EXPECTED_BODY);
}
@Configuration
static class InvalidGroovyMarkupWebConfig extends WebMvcConfigurationSupport {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.groovy();
}
}
@Configuration
static class GroovyMarkupWebConfig extends AbstractWebConfig {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.groovy();
}
@Bean
public GroovyMarkupConfigurer groovyMarkupConfigurer() {
GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
configurer.setResourceLoaderPath("/WEB-INF/");
return configurer;
}
}
}
private static MockHttpServletResponse runTest(Class<?> configClass) throws Exception {
String basePath = "org/springframework/web/servlet/config/annotation";
MockServletContext servletContext = new MockServletContext(basePath);
MockServletConfig servletConfig = new MockServletConfig(servletContext);
@@ -105,8 +256,8 @@ class ViewResolutionIntegrationTests {
static class SampleController {
@GetMapping
public String sample(ModelMap model) {
model.addAttribute("hello", "Hello World!");
String index(ModelMap model) {
model.put("hello", "Hello");
return "index";
}
}
@@ -120,73 +271,4 @@ class ViewResolutionIntegrationTests {
}
}
@Configuration
static class FreeMarkerWebConfig extends AbstractWebConfig {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.freeMarker();
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
return configurer;
}
}
/**
* Test @EnableWebMvc in the presence of a pre-existing ViewResolver.
*/
@Configuration
static class ExistingViewResolverConfig extends AbstractWebConfig {
@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
return new FreeMarkerViewResolver("", ".ftl");
}
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/");
return configurer;
}
}
@Configuration
static class GroovyMarkupWebConfig extends AbstractWebConfig {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.groovy();
}
@Bean
public GroovyMarkupConfigurer groovyMarkupConfigurer() {
GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
configurer.setResourceLoaderPath("/WEB-INF/");
return configurer;
}
}
@Configuration
static class InvalidFreeMarkerWebConfig extends WebMvcConfigurationSupport {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.freeMarker();
}
}
@Configuration
static class InvalidGroovyMarkupWebConfig extends WebMvcConfigurationSupport {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.groovy();
}
}
}

View File

@@ -1 +1 @@
<html><body>${hello}</body></html>
<html><body>${hello}, Java Café</body></html>

View File

@@ -1 +1 @@
html { body(hello) }
html { body(hello + ", Java Café") }