Skip to content

Commit 829d204

Browse files
committed
MVC config doc updates
1 parent 7412d43 commit 829d204

File tree

1 file changed

+94
-155
lines changed

1 file changed

+94
-155
lines changed

src/asciidoc/index.adoc

Lines changed: 94 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -32250,7 +32250,7 @@ available. The sample below shows a subset of what is available:
3225032250

3225132251

3225232252
[[mvc-config-interceptors]]
32253-
==== Configuring Interceptors
32253+
==== Interceptors
3225432254
You can configure `HandlerInterceptors` or `WebRequestInterceptors` to be applied to all
3225532255
incoming requests or restricted to specific URL path patterns.
3225632256

@@ -32295,7 +32295,7 @@ And in XML use the `<mvc:interceptors>` element:
3229532295

3229632296

3229732297
[[mvc-config-content-negotiation]]
32298-
==== Configuring Content Negotiation
32298+
==== Content Negotiation
3229932299
You can configure how Spring MVC determines the requested media types from the client
3230032300
for request mapping as well as for content negotiation purposes. The available options
3230132301
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.
3237132371

3237232372

3237332373
[[mvc-config-view-controller]]
32374-
==== Configuring View Controllers
32374+
==== View Controllers
3237532375
This is a shortcut for defining a `ParameterizableViewController` that immediately
3237632376
forwards to a view when invoked. Use it in static cases when there is no Java controller
3237732377
logic to execute before the view generates the response.
@@ -32402,12 +32402,13 @@ And the same in XML use the `<mvc:view-controller>` element:
3240232402
----
3240332403

3240432404

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.
3240832408

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:
3241132412

3241232413
[source,java,indent=0]
3241332414
[subs="verbatim,quotes"]
@@ -32418,97 +32419,53 @@ useful properties. Bean name and JSP view resolvers can be declared like bellow:
3241832419

3241932420
@Override
3242032421
public void configureViewResolvers(ViewResolverRegistry registry) {
32421-
registry.beanName();
32422-
registry.jsp().prefix("/");
32422+
registry.enableContentNegotiation(new MappingJackson2JsonView());
32423+
registry.jsp();
3242332424
}
3242432425

3242532426
}
3242632427
----
3242732428

32428-
And the same in XML use the `<mvc:view-resolvers>` child elements:
32429+
And the same in XML:
3242932430

3243032431
[source,xml,indent=0]
3243132432
[subs="verbatim,quotes"]
3243232433
----
3243332434
<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>
3243532440
<mvc:jsp />
3243632441
</mvc:view-resolvers>
3243732442
----
3243832443

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.
3248032446

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:
3248332448

3248432449
[source,xml,indent=0]
3248532450
[subs="verbatim,quotes"]
3248632451
----
32452+
3248732453
<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" />
3249232460
</mvc:view-resolvers>
3249332461

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-
3250032462
<mvc:freemarker-configurer>
3250132463
<mvc:template-loader-path location="/freemarker" />
3250232464
</mvc:freemarker-configurer>
3250332465

32504-
<mvc:groovy-configurer />
3250532466
----
3250632467

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:
3251232469

3251332470
[source,java,indent=0]
3251432471
[subs="verbatim,quotes"]
@@ -32519,31 +32476,24 @@ property of the `ContentNegotiatingViewResolver` bean:
3251932476

3252032477
@Override
3252132478
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;
3252432488
}
3252532489

3252632490
}
3252732491
----
3252832492

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-
----
3254332493

3254432494

3254532495
[[mvc-config-static-resources]]
32546-
==== Configuring Serving of Resources
32496+
==== Serving of Resources
3254732497
This option allows static resource requests following a particular URL pattern to be
3254832498
served by a `ResourceHttpRequestHandler` from any of a list of `Resource` locations.
3254932499
This provides a convenient way to serve static resources from locations other than the
@@ -32715,67 +32665,10 @@ Spring JSP tags:
3271532665
<script src="${resourceUrl}/dojo/dojo.js" type="text/javascript"> </script>
3271632666
----
3271732667

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-
----
3277532668

3277632669
[[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
3277932672
of the container's default Servlet), while still allowing static resource requests to be
3278032673
handled by the container's default Servlet. It configures a
3278132674
`DefaultServletHttpRequestHandler` with a URL mapping of "/**" and the lowest priority
@@ -32846,15 +32739,61 @@ Or in XML:
3284632739

3284732740

3284832741

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 {
3285232756

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+
----
3285832797

3285932798

3286032799

0 commit comments

Comments
 (0)