Skip to content

Commit 91bb7d8

Browse files
committed
Use code includes and tabs in MVC Config documentation
This commit also refines the documentation related to `@EnableWebMvc` in order to make it more relevant for modern Boot applications. See spring-projectsgh-22171
1 parent db1010f commit 91bb7d8

File tree

68 files changed

+1588
-881
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1588
-881
lines changed

framework-docs/framework-docs.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ dependencies {
6363
api(project(":spring-jdbc"))
6464
api(project(":spring-jms"))
6565
api(project(":spring-web"))
66+
api(project(":spring-webmvc"))
67+
api(project(":spring-context-support"))
6668

6769
api("org.jetbrains.kotlin:kotlin-stdlib")
6870
api("jakarta.jms:jakarta.jms-api")
6971
api("jakarta.servlet:jakarta.servlet-api")
7072
api("org.apache.commons:commons-dbcp2:2.11.0")
7173
api("com.mchange:c3p0:0.9.5.5")
74+
api("com.fasterxml.jackson.core:jackson-databind")
75+
api("com.fasterxml.jackson.module:jackson-module-parameter-names")
7276

7377
implementation(project(":spring-core-test"))
7478
implementation("org.assertj:assertj-core")

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/advanced-java.adoc

+1-24
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,7 @@ For advanced mode, you can remove `@EnableWebMvc` and extend directly from
1212
`DelegatingWebMvcConfiguration` instead of implementing `WebMvcConfigurer`,
1313
as the following example shows:
1414

15-
[tabs]
16-
======
17-
Java::
18-
+
19-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
20-
----
21-
@Configuration
22-
public class WebConfig extends DelegatingWebMvcConfiguration {
23-
24-
// ...
25-
}
26-
----
27-
28-
Kotlin::
29-
+
30-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
31-
----
32-
@Configuration
33-
class WebConfig : DelegatingWebMvcConfiguration() {
34-
35-
// ...
36-
}
37-
----
38-
======
15+
include-code::./WebConfiguration[tag=snippet,indent=0]
3916

4017
You can keep existing methods in `WebConfig`, but you can now also override bean declarations
4118
from the base class, and you can still have any number of other `WebMvcConfigurer` implementations on

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/advanced-xml.adoc

+1-33
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,7 @@ The MVC namespace does not have an advanced mode. If you need to customize a pro
55
a bean that you cannot change otherwise, you can use the `BeanPostProcessor` lifecycle
66
hook of the Spring `ApplicationContext`, as the following example shows:
77

8-
[tabs]
9-
======
10-
Java::
11-
+
12-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
13-
----
14-
@Component
15-
public class MyPostProcessor implements BeanPostProcessor {
16-
17-
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
18-
// ...
19-
}
20-
}
21-
----
22-
23-
Kotlin::
24-
+
25-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
26-
----
27-
@Component
28-
class MyPostProcessor : BeanPostProcessor {
29-
30-
override fun postProcessBeforeInitialization(bean: Any, name: String): Any {
31-
// ...
32-
}
33-
}
34-
----
35-
======
36-
8+
include-code::./MyPostProcessor[tag=snippet,indent=0]
379

3810
Note that you need to declare `MyPostProcessor` as a bean, either explicitly in XML or
3911
by letting it be detected through a `<component-scan/>` declaration.
40-
41-
42-
43-

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/content-negotiation.adoc

+2-52
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,9 @@ strategy over path extensions. See
1313
xref:web/webmvc/mvc-controller/ann-requestmapping.adoc#mvc-ann-requestmapping-suffix-pattern-match[Suffix Match] and xref:web/webmvc/mvc-controller/ann-requestmapping.adoc#mvc-ann-requestmapping-rfd[Suffix Match and RFD] for
1414
more details.
1515

16-
In Java configuration, you can customize requested content type resolution, as the
17-
following example shows:
16+
You can customize requested content type resolution, as the following example shows:
1817

19-
[tabs]
20-
======
21-
Java::
22-
+
23-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
24-
----
25-
@Configuration
26-
@EnableWebMvc
27-
public class WebConfig implements WebMvcConfigurer {
28-
29-
@Override
30-
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
31-
configurer.mediaType("json", MediaType.APPLICATION_JSON);
32-
configurer.mediaType("xml", MediaType.APPLICATION_XML);
33-
}
34-
}
35-
----
36-
37-
Kotlin::
38-
+
39-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
40-
----
41-
@Configuration
42-
@EnableWebMvc
43-
class WebConfig : WebMvcConfigurer {
44-
45-
override fun configureContentNegotiation(configurer: ContentNegotiationConfigurer) {
46-
configurer.mediaType("json", MediaType.APPLICATION_JSON)
47-
configurer.mediaType("xml", MediaType.APPLICATION_XML)
48-
}
49-
}
50-
----
51-
======
52-
53-
54-
The following example shows how to achieve the same configuration in XML:
55-
56-
[source,xml,indent=0,subs="verbatim,quotes"]
57-
----
58-
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
59-
60-
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
61-
<property name="mediaTypes">
62-
<value>
63-
json=application/json
64-
xml=application/xml
65-
</value>
66-
</property>
67-
</bean>
68-
----
18+
include-code::./WebConfiguration[tag=snippet,indent=0]
6919

7020

7121

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/conversion.adoc

+3-106
Original file line numberDiff line numberDiff line change
@@ -6,119 +6,16 @@
66
By default, formatters for various number and date types are installed, along with support
77
for customization via `@NumberFormat` and `@DateTimeFormat` on fields.
88

9-
To register custom formatters and converters in Java config, use the following:
9+
To register custom formatters and converters, use the following:
1010

11-
[tabs]
12-
======
13-
Java::
14-
+
15-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
16-
----
17-
@Configuration
18-
@EnableWebMvc
19-
public class WebConfig implements WebMvcConfigurer {
20-
21-
@Override
22-
public void addFormatters(FormatterRegistry registry) {
23-
// ...
24-
}
25-
}
26-
----
27-
28-
Kotlin::
29-
+
30-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
31-
----
32-
@Configuration
33-
@EnableWebMvc
34-
class WebConfig : WebMvcConfigurer {
35-
36-
override fun addFormatters(registry: FormatterRegistry) {
37-
// ...
38-
}
39-
}
40-
----
41-
======
42-
43-
To do the same in XML config, use the following:
44-
45-
[source,xml,indent=0,subs="verbatim,quotes"]
46-
----
47-
<?xml version="1.0" encoding="UTF-8"?>
48-
<beans xmlns="http://www.springframework.org/schema/beans"
49-
xmlns:mvc="http://www.springframework.org/schema/mvc"
50-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
51-
xsi:schemaLocation="
52-
http://www.springframework.org/schema/beans
53-
https://www.springframework.org/schema/beans/spring-beans.xsd
54-
http://www.springframework.org/schema/mvc
55-
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
56-
57-
<mvc:annotation-driven conversion-service="conversionService"/>
58-
59-
<bean id="conversionService"
60-
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
61-
<property name="converters">
62-
<set>
63-
<bean class="org.example.MyConverter"/>
64-
</set>
65-
</property>
66-
<property name="formatters">
67-
<set>
68-
<bean class="org.example.MyFormatter"/>
69-
<bean class="org.example.MyAnnotationFormatterFactory"/>
70-
</set>
71-
</property>
72-
<property name="formatterRegistrars">
73-
<set>
74-
<bean class="org.example.MyFormatterRegistrar"/>
75-
</set>
76-
</property>
77-
</bean>
78-
79-
</beans>
80-
----
11+
include-code::./WebConfiguration[tag=snippet,indent=0]
8112

8213
By default Spring MVC considers the request Locale when parsing and formatting date
8314
values. This works for forms where dates are represented as Strings with "input" form
8415
fields. For "date" and "time" form fields, however, browsers use a fixed format defined
8516
in the HTML spec. For such cases date and time formatting can be customized as follows:
8617

87-
[tabs]
88-
======
89-
Java::
90-
+
91-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
92-
----
93-
@Configuration
94-
@EnableWebMvc
95-
public class WebConfig implements WebMvcConfigurer {
96-
97-
@Override
98-
public void addFormatters(FormatterRegistry registry) {
99-
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
100-
registrar.setUseIsoFormat(true);
101-
registrar.registerFormatters(registry);
102-
}
103-
}
104-
----
105-
106-
Kotlin::
107-
+
108-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
109-
----
110-
@Configuration
111-
@EnableWebMvc
112-
class WebConfig : WebMvcConfigurer {
113-
114-
override fun addFormatters(registry: FormatterRegistry) {
115-
val registrar = DateTimeFormatterRegistrar()
116-
registrar.setUseIsoFormat(true)
117-
registrar.registerFormatters(registry)
118-
}
119-
}
120-
----
121-
======
18+
include-code::./DateTimeWebConfiguration[tag=snippet,indent=0]
12219

12320
NOTE: See xref:core/validation/format.adoc#format-FormatterRegistrar-SPI[the `FormatterRegistrar` SPI]
12421
and the `FormattingConversionServiceFactoryBean` for more information on when to use

framework-docs/modules/ROOT/pages/web/webmvc/mvc-config/customize.adoc

+1-27
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,7 @@
66
In Java configuration, you can implement the `WebMvcConfigurer` interface, as the
77
following example shows:
88

9-
[tabs]
10-
======
11-
Java::
12-
+
13-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
14-
----
15-
@Configuration
16-
@EnableWebMvc
17-
public class WebConfig implements WebMvcConfigurer {
18-
19-
// Implement configuration methods...
20-
}
21-
----
22-
23-
Kotlin::
24-
+
25-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
26-
----
27-
@Configuration
28-
@EnableWebMvc
29-
class WebConfig : WebMvcConfigurer {
30-
31-
// Implement configuration methods...
32-
}
33-
----
34-
======
35-
9+
include-code::./WebConfiguration[tag=snippet,indent=0]
3610

3711
In XML, you can check attributes and sub-elements of `<mvc:annotation-driven/>`. You can
3812
view the https://schema.spring.io/mvc/spring-mvc.xsd[Spring MVC XML schema] or use

0 commit comments

Comments
 (0)