@@ -32250,7 +32250,7 @@ available. The sample below shows a subset of what is available:
32250
32250
32251
32251
32252
32252
[[mvc-config-interceptors]]
32253
- ==== Configuring Interceptors
32253
+ ==== Interceptors
32254
32254
You can configure `HandlerInterceptors` or `WebRequestInterceptors` to be applied to all
32255
32255
incoming requests or restricted to specific URL path patterns.
32256
32256
@@ -32295,7 +32295,7 @@ And in XML use the `<mvc:interceptors>` element:
32295
32295
32296
32296
32297
32297
[[mvc-config-content-negotiation]]
32298
- ==== Configuring Content Negotiation
32298
+ ==== Content Negotiation
32299
32299
You can configure how Spring MVC determines the requested media types from the client
32300
32300
for request mapping as well as for content negotiation purposes. The available options
32301
32301
are to check the file extension in the request URI, the "Accept" header, a request
@@ -32371,7 +32371,7 @@ JSON) if no content types were requested.
32371
32371
32372
32372
32373
32373
[[mvc-config-view-controller]]
32374
- ==== Configuring View Controllers
32374
+ ==== View Controllers
32375
32375
This is a shortcut for defining a `ParameterizableViewController` that immediately
32376
32376
forwards to a view when invoked. Use it in static cases when there is no Java controller
32377
32377
logic to execute before the view generates the response.
@@ -32402,12 +32402,13 @@ 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 .
32405
+ [[mvc-config-view-resolvers ]]
32406
+ ==== View Resolvers
32407
+ The MVC config simplifies the registration of view resolvers .
32408
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:
32409
+ The following is a Java config example that configures content negotiation view
32410
+ resolution using FreeMarker HTML templates and Jackson as a default `View` for
32411
+ JSON rendering:
32411
32412
32412
32413
[source,java,indent=0]
32413
32414
[subs="verbatim,quotes"]
@@ -32418,97 +32419,53 @@ useful properties. Bean name and JSP view resolvers can be declared like bellow:
32418
32419
32419
32420
@Override
32420
32421
public void configureViewResolvers(ViewResolverRegistry registry) {
32421
- registry.beanName( );
32422
- registry.jsp().prefix("/") ;
32422
+ registry.enableContentNegotiation(new MappingJackson2JsonView() );
32423
+ registry.jsp();
32423
32424
}
32424
32425
32425
32426
}
32426
32427
----
32427
32428
32428
- And the same in XML use the `<mvc:view-resolvers>` child elements :
32429
+ And the same in XML:
32429
32430
32430
32431
[source,xml,indent=0]
32431
32432
[subs="verbatim,quotes"]
32432
32433
----
32433
32434
<mvc:view-resolvers>
32434
- <mvc:bean-name />
32435
+ <mvc:content-negotiation>
32436
+ <mvc:default-views>
32437
+ <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
32438
+ </mvc:default-views>
32439
+ </mvc:content-negotiation>
32435
32440
<mvc:jsp />
32436
32441
</mvc:view-resolvers>
32437
32442
----
32438
32443
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
- ----
32444
+ Note however that FreeMarker, Velocity, Tiles, and Groovy Markup also require
32445
+ configuration of the underlying view technology.
32480
32446
32481
- For XML, in addition to the `<mvc:view-resolvers>` child elements, shortcut alternative
32482
- to declaring configurer beans directly are also provided:
32447
+ The MVC namespace provides dedicated elements. For example with FreeMarker:
32483
32448
32484
32449
[source,xml,indent=0]
32485
32450
[subs="verbatim,quotes"]
32486
32451
----
32452
+
32487
32453
<mvc:view-resolvers>
32488
- <mvc:tiles />
32489
- <mvc:velocity />
32490
- <mvc:freemarker />
32491
- <mvc:groovy />
32454
+ <mvc:content-negotiation>
32455
+ <mvc:default-views>
32456
+ <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
32457
+ </mvc:default-views>
32458
+ </mvc:content-negotiation>
32459
+ <mvc:freemarker cache="false" />
32492
32460
</mvc:view-resolvers>
32493
32461
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
32462
<mvc:freemarker-configurer>
32501
32463
<mvc:template-loader-path location="/freemarker" />
32502
32464
</mvc:freemarker-configurer>
32503
32465
32504
- <mvc:groovy-configurer />
32505
32466
----
32506
32467
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:
32468
+ In Java config simply add the respective "Configurer" bean:
32512
32469
32513
32470
[source,java,indent=0]
32514
32471
[subs="verbatim,quotes"]
@@ -32519,31 +32476,24 @@ property of the `ContentNegotiatingViewResolver` bean:
32519
32476
32520
32477
@Override
32521
32478
public void configureViewResolvers(ViewResolverRegistry registry) {
32522
- registry.contentNegotiating(new MappingJackson2JsonView());
32523
- registry.jsp();
32479
+ registry.enableContentNegotiation(new MappingJackson2JsonView());
32480
+ registry.freeMarker().cache(false);
32481
+ }
32482
+
32483
+ @Bean
32484
+ public FreeMarkerConfigurer freeMarkerConfigurer() {
32485
+ FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
32486
+ configurer.setTemplateLoaderPath("/WEB-INF/");
32487
+ return configurer;
32524
32488
}
32525
32489
32526
32490
}
32527
32491
----
32528
32492
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
32493
32544
32494
32545
32495
[[mvc-config-static-resources]]
32546
- ==== Configuring Serving of Resources
32496
+ ==== Serving of Resources
32547
32497
This option allows static resource requests following a particular URL pattern to be
32548
32498
served by a `ResourceHttpRequestHandler` from any of a list of `Resource` locations.
32549
32499
This provides a convenient way to serve static resources from locations other than the
@@ -32715,67 +32665,10 @@ Spring JSP tags:
32715
32665
<script src="${resourceUrl}/dojo/dojo.js" type="text/javascript"> </script>
32716
32666
----
32717
32667
32718
- [[mvc-config-path-matching]]
32719
- ==== Configuring Path Matching
32720
- This is a shortcut for configuring `RequestMappingHandlerMapping` path matching properties, without
32721
- having to <<mvc-config-advanced-java,override this Bean with an advanced setup>>.
32722
-
32723
- An example of enabling pattern match features and using custom matchers, in Java:
32724
-
32725
- [source,java,indent=0]
32726
- [subs="verbatim,quotes"]
32727
- ----
32728
- @Configuration
32729
- @EnableWebMvc
32730
- public class WebConfig extends WebMvcConfigurerAdapter {
32731
-
32732
- @Override
32733
- public void configurePathMatch(PathMatchConfigurer configurer) {
32734
- configurer
32735
- .setUseSuffixPatternMatch(true)
32736
- .setUseTrailingSlashMatch(false)
32737
- .setUseRegisteredSuffixPatternMatch(true)
32738
- .setPathMatcher(antPathMatcher())
32739
- .setUrlPathHelper(urlPathHelper());
32740
- }
32741
-
32742
- @Bean
32743
- public UrlPathHelper urlPathHelper() {
32744
- //...
32745
- }
32746
-
32747
- @Bean
32748
- public PathMatcher antPathMatcher() {
32749
- //...
32750
- }
32751
-
32752
- }
32753
- ----
32754
-
32755
- For more details, check out the
32756
- {javadoc-baseurl}/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.html[PathMatchConfigurer] API.
32757
-
32758
- And the same in XML, use the `<mvc:path-matching>` element:
32759
-
32760
- [source,xml,indent=0]
32761
- [subs="verbatim,quotes"]
32762
- ----
32763
- <mvc:annotation-driven>
32764
- <mvc:path-matching
32765
- suffix-pattern="true"
32766
- trailing-slash="false"
32767
- registered-suffixes-only="true"
32768
- path-helper="pathHelper"
32769
- path-matcher="pathMatcher" />
32770
- </mvc:annotation-driven>
32771
-
32772
- <bean id="pathHelper" class="org.example.app.MyPathHelper" />
32773
- <bean id="pathMatcher" class="org.example.app.MyPathMatcher" />
32774
- ----
32775
32668
32776
32669
[[mvc-default-servlet-handler]]
32777
- ==== mvc:default-servlet-handler
32778
- This tag allows for mapping the `DispatcherServlet` to "/" (thus overriding the mapping
32670
+ ==== Falling Back On the "Default" Servlet To Serve Resources
32671
+ This allows for mapping the `DispatcherServlet` to "/" (thus overriding the mapping
32779
32672
of the container's default Servlet), while still allowing static resource requests to be
32780
32673
handled by the container's default Servlet. It configures a
32781
32674
`DefaultServletHttpRequestHandler` with a URL mapping of "/**" and the lowest priority
@@ -32846,15 +32739,61 @@ Or in XML:
32846
32739
32847
32740
32848
32741
32849
- [[mvc-resources]]
32850
- ==== More Spring Web MVC Resources
32851
- See the following links and pointers for more resources about Spring Web MVC:
32742
+ [[mvc-config-path-matching]]
32743
+ ==== Path Matching
32744
+ This allows customizing various settings related to URL mapping and path matching.
32745
+ For details on the individual options check out the
32746
+ {javadoc-baseurl}/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.html[PathMatchConfigurer] API.
32747
+
32748
+ Below is an example in Java config:
32749
+
32750
+ [source,java,indent=0]
32751
+ [subs="verbatim,quotes"]
32752
+ ----
32753
+ @Configuration
32754
+ @EnableWebMvc
32755
+ public class WebConfig extends WebMvcConfigurerAdapter {
32852
32756
32853
- * There are many excellent articles and tutorials that show how to build web
32854
- applications with Spring MVC. Read them at the http://spring.io/docs[Spring
32855
- Documentation] page.
32856
- * "Expert Spring Web MVC and Web Flow" by Seth Ladd and others (published by Apress) is
32857
- an excellent hard copy source of Spring Web MVC goodness.
32757
+ @Override
32758
+ public void configurePathMatch(PathMatchConfigurer configurer) {
32759
+ configurer
32760
+ .setUseSuffixPatternMatch(true)
32761
+ .setUseTrailingSlashMatch(false)
32762
+ .setUseRegisteredSuffixPatternMatch(true)
32763
+ .setPathMatcher(antPathMatcher())
32764
+ .setUrlPathHelper(urlPathHelper());
32765
+ }
32766
+
32767
+ @Bean
32768
+ public UrlPathHelper urlPathHelper() {
32769
+ //...
32770
+ }
32771
+
32772
+ @Bean
32773
+ public PathMatcher antPathMatcher() {
32774
+ //...
32775
+ }
32776
+
32777
+ }
32778
+ ----
32779
+
32780
+ And the same in XML, use the `<mvc:path-matching>` element:
32781
+
32782
+ [source,xml,indent=0]
32783
+ [subs="verbatim,quotes"]
32784
+ ----
32785
+ <mvc:annotation-driven>
32786
+ <mvc:path-matching
32787
+ suffix-pattern="true"
32788
+ trailing-slash="false"
32789
+ registered-suffixes-only="true"
32790
+ path-helper="pathHelper"
32791
+ path-matcher="pathMatcher" />
32792
+ </mvc:annotation-driven>
32793
+
32794
+ <bean id="pathHelper" class="org.example.app.MyPathHelper" />
32795
+ <bean id="pathMatcher" class="org.example.app.MyPathMatcher" />
32796
+ ----
32858
32797
32859
32798
32860
32799
0 commit comments