@@ -32402,6 +32402,145 @@ And the same in XML use the `<mvc:view-controller>` element:
32402
32402
----
32403
32403
32404
32404
32405
+ [[mvc-config-view-resolution]]
32406
+ ==== Configuring View Resolution
32407
+ This is a shortcut for defining `ViewResolver` beans that can resolve views by name.
32408
+
32409
+ The API allows to declare view resolvers with default values, and to customize most
32410
+ useful properties. Bean name and JSP view resolvers can be declared like bellow:
32411
+
32412
+ [source,java,indent=0]
32413
+ [subs="verbatim,quotes"]
32414
+ ----
32415
+ @Configuration
32416
+ @EnableWebMvc
32417
+ public class WebConfig extends WebMvcConfigurerAdapter {
32418
+
32419
+ @Override
32420
+ public void configureViewResolvers(ViewResolverRegistry registry) {
32421
+ registry.beanName();
32422
+ registry.jsp().prefix("/");
32423
+ }
32424
+
32425
+ }
32426
+ ----
32427
+
32428
+ And the same in XML use the `<mvc:view-resolvers>` child elements:
32429
+
32430
+ [source,xml,indent=0]
32431
+ [subs="verbatim,quotes"]
32432
+ ----
32433
+ <mvc:view-resolvers>
32434
+ <mvc:bean-name />
32435
+ <mvc:jsp />
32436
+ </mvc:view-resolvers>
32437
+ ----
32438
+
32439
+ Configuring Tiles, Velocity, FreeMarker and Groovy view resolution requires
32440
+ declaring both `ViewResolver` (configured thanks to the `ViewResolverRegistry`)
32441
+ and configurer beans:
32442
+
32443
+ [source,java,indent=0]
32444
+ [subs="verbatim,quotes"]
32445
+ ----
32446
+ @Configuration
32447
+ @EnableWebMvc
32448
+ public class WebConfig extends WebMvcConfigurerAdapter {
32449
+
32450
+ @Override
32451
+ public void configureViewResolvers(ViewResolverRegistry registry) {
32452
+ registry.tiles();
32453
+ registry.velocity();
32454
+ registry.freeMarker().suffix(".fmt").cache(false);
32455
+ registry.groovy();
32456
+ }
32457
+
32458
+ @Bean
32459
+ public TilesConfigurer tilesConfigurer() {
32460
+ return new TilesConfigurer();
32461
+ }
32462
+
32463
+ @Bean
32464
+ public VelocityConfigurer velocityConfigurer() {
32465
+ return new VelocityConfigurer();
32466
+ }
32467
+
32468
+ @Bean
32469
+ public FreeMarkerConfigurer freeMarkerConfigurer() {
32470
+ return new FreeMarkerConfigurer();
32471
+ }
32472
+
32473
+ @Bean
32474
+ public GroovyMarkupConfigurer groovyMarkupConfigurer() {
32475
+ return new GroovyMarkupConfigurer();
32476
+ }
32477
+
32478
+ }
32479
+ ----
32480
+
32481
+ For XML, in addition to the `<mvc:view-resolvers>` child elements, shortcut alternative
32482
+ to declaring configurer beans directly are also provided:
32483
+
32484
+ [source,xml,indent=0]
32485
+ [subs="verbatim,quotes"]
32486
+ ----
32487
+ <mvc:view-resolvers>
32488
+ <mvc:tiles />
32489
+ <mvc:velocity />
32490
+ <mvc:freemarker />
32491
+ <mvc:groovy />
32492
+ </mvc:view-resolvers>
32493
+
32494
+ <mvc:tiles-configurer>
32495
+ <mvc:definitions location="/tiles/tiles1.xml" />
32496
+ </mvc:tiles-configurer>
32497
+
32498
+ <mvc:velocity-configurer resource-loader-path="/velocity" />
32499
+
32500
+ <mvc:freemarker-configurer>
32501
+ <mvc:template-loader-path location="/freemarker" />
32502
+ </mvc:freemarker-configurer>
32503
+
32504
+ <mvc:groovy-configurer />
32505
+ ----
32506
+
32507
+ It is also possible to declare a `ContentNegotiatingViewResolver` bean thanks to the
32508
+ `ViewResolverRegistry`. The `ContentNegotiationManager` used to determine requested media
32509
+ types is configured as described in <<mvc-config-content-negotiation>>.
32510
+ All other view resolvers declared are automatically added to the `viewResolvers`
32511
+ property of the `ContentNegotiatingViewResolver` bean:
32512
+
32513
+ [source,java,indent=0]
32514
+ [subs="verbatim,quotes"]
32515
+ ----
32516
+ @Configuration
32517
+ @EnableWebMvc
32518
+ public class WebConfig extends WebMvcConfigurerAdapter {
32519
+
32520
+ @Override
32521
+ public void configureViewResolvers(ViewResolverRegistry registry) {
32522
+ registry.contentNegotiating(new MappingJackson2JsonView());
32523
+ registry.jsp();
32524
+ }
32525
+
32526
+ }
32527
+ ----
32528
+
32529
+ And the same in XML use the `<mvc:content-negotiation>` element:
32530
+
32531
+ [source,xml,indent=0]
32532
+ [subs="verbatim,quotes"]
32533
+ ----
32534
+ <mvc:view-resolvers>
32535
+ <mvc:content-negotiation use-not-acceptable="true">
32536
+ <mvc:default-views>
32537
+ <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
32538
+ </mvc:default-views>
32539
+ </mvc:content-negotiation>
32540
+ <mvc:jsp />
32541
+ </mvc:view-resolvers>
32542
+ ----
32543
+
32405
32544
32406
32545
[[mvc-config-static-resources]]
32407
32546
==== Configuring Serving of Resources
0 commit comments