Skip to content

Commit 10a4c2c

Browse files
committed
Remove FreeMarker/Velocity/TilesConfigurer MVC config
After some further discussion: The MVC config simplifies ViewResolver configuration especially where content negotiation view resolution is involved. The configuration of the underlying view technology however is kept completely separate. In the case of the MVC namespace, dedicated top-level freemarker, velocity, and tiles namespace elements are provided. In the case of the MVC Java config, applications simply declare FreeMarkerConfigurer, VelocityConfigurer, or TilesConfigurer beans respectively. Issue: SPR-7093
1 parent be4d73e commit 10a4c2c

File tree

11 files changed

+63
-531
lines changed

11 files changed

+63
-531
lines changed

spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* @author Sebastien Deleuze
5252
*/
5353
@RunWith(SpringJUnit4ClassRunner.class)
54-
@WebAppConfiguration("src/test/resources/META-INF/web-resources")
54+
@WebAppConfiguration("classpath:META-INF/web-resources")
5555
@ContextHierarchy({
5656
@ContextConfiguration(classes = RootConfig.class),
5757
@ContextConfiguration(classes = WebConfig.class)
@@ -100,7 +100,7 @@ public PersonDao personDao() {
100100

101101
@Configuration
102102
@EnableWebMvc
103-
static class WebConfig extends WebMvcConfigurerAdapter implements TilesWebMvcConfigurer {
103+
static class WebConfig extends WebMvcConfigurerAdapter {
104104

105105
@Autowired
106106
private RootConfig rootConfig;
@@ -130,9 +130,11 @@ public void configureViewResolvers(ViewResolverRegistry registry) {
130130
registry.tiles();
131131
}
132132

133-
@Override
134-
public void configureTiles(TilesConfigurer configurer) {
133+
@Bean
134+
public TilesConfigurer tilesConfigurer() {
135+
TilesConfigurer configurer = new TilesConfigurer();
135136
configurer.setDefinitions("/WEB-INF/**/tiles.xml");
137+
return configurer;
136138
}
137139
}
138140

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
* }
3434
* </pre>
3535
*
36-
* <p>As of 4.1 this annotation may also import {@link WebMvcFreeMarkerConfiguration},
37-
* {@link WebMvcVelocityConfiguration}, or {@link WebMvcTilesConfiguration} if
38-
* those libraries are found on the classpath.
39-
*
4036
* <p>To customize the imported configuration, implement the interface
4137
* {@link WebMvcConfigurer} or more likely extend the empty method base class
4238
* {@link WebMvcConfigurerAdapter} and override individual methods, e.g.:
@@ -61,10 +57,6 @@
6157
* }
6258
* </pre>
6359
*
64-
* <p>To customize the FreeMarker, Velocity, or Tiles configuration, additionally
65-
* implement {@link FreeMarkerWebMvcConfigurer}, {@link VelocityWebMvcConfigurer},
66-
* and/or {@link TilesWebMvcConfigurer}.
67-
*
6860
* <p>If {@link WebMvcConfigurer} does not expose some advanced setting that
6961
* needs to be configured, consider removing the {@code @EnableWebMvc}
7062
* annotation and extending directly from {@link WebMvcConfigurationSupport}, e.g.:
@@ -87,23 +79,15 @@
8779
* }
8880
* </pre>
8981
*
90-
* <p>When the {@code @EnableWebMvc} annotation is removed, the FreeMarker,
91-
* Velocity, and Tiles configuration is no longer automatically imported and need
92-
* to be imported explicitly.
93-
*
9482
* @author Dave Syer
9583
* @author Rossen Stoyanchev
96-
* @author Sebastien Deleuze
9784
* @since 3.1
9885
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurer
9986
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
100-
* @see org.springframework.web.servlet.config.annotation.FreeMarkerWebMvcConfigurer
101-
* @see org.springframework.web.servlet.config.annotation.VelocityWebMvcConfigurer
102-
* @see org.springframework.web.servlet.config.annotation.TilesWebMvcConfigurer
10387
*/
10488
@Retention(RetentionPolicy.RUNTIME)
10589
@Target(ElementType.TYPE)
10690
@Documented
107-
@Import({DelegatingWebMvcConfiguration.class, ViewConfigurationImportSelector.class})
91+
@Import({DelegatingWebMvcConfiguration.class})
10892
public @interface EnableWebMvc {
10993
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/FreeMarkerWebMvcConfigurer.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/TilesWebMvcConfigurer.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/VelocityWebMvcConfigurer.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewConfigurationImportSelector.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,27 @@ private void initContentNegotiatingViewResolver(View[] defaultViews) {
130130
}
131131

132132
/**
133-
* Enable view resolution by forwarding to JSP pages with a default view name
134-
* prefix of "/WEB-INF/" and a default suffix of ".jsp".
133+
* Register JSP view resolver using a default view name prefix of "/WEB-INF/"
134+
* and a default suffix of ".jsp".
135135
*
136-
* <p>This method may be invoked multiple and each call will register a
137-
* separate ViewResolver instance. Note that since it's not easy to determine
136+
* <p>When this method is invoked more than once, each call will register a
137+
* new ViewResolver instance. Note that since it's not easy to determine
138138
* if a JSP exists without forwarding to it, using multiple JSP-based view
139139
* resolvers only makes sense in combination with the "viewNames" property
140-
* that indicates which view names are handled by which resolver.
140+
* on the resolver indicating which view names are handled by which resolver.
141141
*/
142142
public UrlBasedViewResolverRegistration jsp() {
143143
return jsp("/WEB-INF/", ".jsp");
144144
}
145145

146146
/**
147-
* Enable view resolution by forwarding to JSP pages with the specified
148-
* prefix and suffix.
147+
* Register JSP view resolver with the specified prefix and suffix.
149148
*
150-
* <p>This method may be invoked multiple and each call will register a
151-
* separate ViewResolver instance. Note that since it's not easy to determine
149+
* <p>When this method is invoked more than once, each call will register a
150+
* new ViewResolver instance. Note that since it's not easy to determine
152151
* if a JSP exists without forwarding to it, using multiple JSP-based view
153152
* resolvers only makes sense in combination with the "viewNames" property
154-
* that indicates which view names are handled by which resolver.
153+
* on the resolver indicating which view names are handled by which resolver.
155154
*/
156155
public UrlBasedViewResolverRegistration jsp(String prefix, String suffix) {
157156
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
@@ -162,67 +161,63 @@ public UrlBasedViewResolverRegistration jsp(String prefix, String suffix) {
162161
}
163162

164163
/**
165-
* Enable Tiles-based view resolution.
164+
* Register Tiles 3.x view resolver.
166165
*
167-
* <p>By default tiles definitions are expected to be in "/WEB-INF/tiles.xml".
168-
* To change that and other Tiles-related options please also implement the
169-
* interface {@link TilesWebMvcConfigurer}.
166+
* <p><strong>Note</strong> that you must also configure Tiles by adding a
167+
* {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer} bean.
170168
*/
171169
public UrlBasedViewResolverRegistration tiles() {
172170
if (this.applicationContext != null && !hasBeanOfType(TilesConfigurer.class)) {
173-
throw new BeanInitializationException(
174-
"It looks like you're trying to configure Tiles view resolution. " +
175-
"If not using @EnableWebMvc you must import WebMvcTilesConfiguration, " +
176-
"or declare your own TilesConfigurer bean.");
171+
throw new BeanInitializationException("In addition to a Tiles view resolver " +
172+
"there must also be a single TilesConfigurer bean in this web application context " +
173+
"(or its parent).");
177174
}
178175
TilesRegistration registration = new TilesRegistration();
179176
this.viewResolvers.add(registration.getViewResolver());
180177
return registration;
181178
}
182179

183180
/**
184-
* Enable FreeMarker-based view resolution with an empty default view name
181+
* Register a FreeMarker view resolver with an empty default view name
185182
* prefix and a default suffix of ".ftl".
186183
*
187-
* <p>By default the FreeMarker template loader path is set to "/WEB-INF/".
188-
* To change that and other FreeMarker-related options please also implement
189-
* the interface {@link FreeMarkerWebMvcConfigurer}.
184+
* <p><strong>Note</strong> that you must also configure FreeMarker by adding a
185+
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} bean.
190186
*/
191187
public UrlBasedViewResolverRegistration freeMarker() {
192188
if (this.applicationContext != null && !hasBeanOfType(FreeMarkerConfigurer.class)) {
193-
throw new BeanInitializationException(
194-
"It looks like you're trying to configure FreeMarker view resolution. " +
195-
"If not using @EnableWebMvc you must import WebMvcFreeMarkerConfiguration, " +
196-
"or declare your own FreeMarkerConfigurer bean.");
189+
throw new BeanInitializationException("In addition to a FreeMarker view resolver " +
190+
"there must also be a single FreeMarkerConfig bean in this web application context " +
191+
"(or its parent): FreeMarkerConfigurer is the usual implementation. " +
192+
"This bean may be given any name.");
197193
}
198194
FreeMarkerRegistration registration = new FreeMarkerRegistration();
199195
this.viewResolvers.add(registration.getViewResolver());
200196
return registration;
201197
}
202198

203199
/**
204-
* Enable Velocity-based view resolution with an empty default view name
200+
* Register Velocity view resolver with an empty default view name
205201
* prefix, a default suffix of ".vm".
206202
*
207-
* <p>By default the Velocity resource loader path is set to "/WEB-INF/".
208-
* To change that and other Velocity-related options please also implement
209-
* the interface {@link VelocityWebMvcConfigurer}.
203+
* <p><strong>Note</strong> that you must also configure Velocity by adding a
204+
* {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer} bean.
210205
*/
211206
public UrlBasedViewResolverRegistration velocity() {
212207
if (this.applicationContext != null && !hasBeanOfType(VelocityConfigurer.class)) {
213-
throw new BeanInitializationException(
214-
"It looks like you're trying to configure Velocity view resolution. " +
215-
"If not using @EnableWebMvc you must import WebMvcVelocityConfiguration, " +
216-
"or declare your own VelocityConfigurer bean.");
208+
throw new BeanInitializationException("In addition to a Velocity view resolver " +
209+
"there must also be a single VelocityConfig bean in this web application context " +
210+
"(or its parent): VelocityConfigurer is the usual implementation. " +
211+
"This bean may be given any name.");
217212
}
218213
VelocityRegistration registration = new VelocityRegistration();
219214
this.viewResolvers.add(registration.getViewResolver());
220215
return registration;
221216
}
222217

223218
/**
224-
* Enable the ability to map view names returned from controllers to
225-
* {@link org.springframework.web.servlet.View} beans.
219+
* Register a bean name view resolver that interprets view names as the names
220+
* of {@link org.springframework.web.servlet.View} beans.
226221
*/
227222
public void beanName() {
228223
BeanNameViewResolver resolver = new BeanNameViewResolver();

0 commit comments

Comments
 (0)