diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/context-hierarchy.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/context-hierarchy.adoc index 84f233adb49..e2d06ae0934 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/context-hierarchy.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/context-hierarchy.adoc @@ -22,89 +22,13 @@ The following image shows this relationship: image::mvc-context-hierarchy.png[] -The following example configures a `WebApplicationContext` hierarchy: +The following example configures a `WebApplicationContext` hierarchy, and the equivalent `web.xml`: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { - - @Override - protected Class[] getRootConfigClasses() { - return new Class[] { RootConfig.class }; - } - - @Override - protected Class[] getServletConfigClasses() { - return new Class[] { App1Config.class }; - } - - @Override - protected String[] getServletMappings() { - return new String[] { "/app1/*" }; - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - class MyWebAppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() { - - override fun getRootConfigClasses(): Array> { - return arrayOf(RootConfig::class.java) - } - - override fun getServletConfigClasses(): Array> { - return arrayOf(App1Config::class.java) - } - - override fun getServletMappings(): Array { - return arrayOf("/app1/*") - } - } ----- -====== +include-code::./MyWebAppInitializer[tag=snippet,indent=0] TIP: If an application context hierarchy is not required, applications can return all configuration through `getRootConfigClasses()` and `null` from `getServletConfigClasses()`. -The following example shows the `web.xml` equivalent: - -[source,xml,indent=0,subs="verbatim,quotes"] ----- - - - - org.springframework.web.context.ContextLoaderListener - - - - contextConfigLocation - /WEB-INF/root-context.xml - - - - app1 - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - /WEB-INF/app1-context.xml - - 1 - - - - app1 - /app1/* - - - ----- TIP: If an application context hierarchy is not required, applications may configure a "`root`" context only and leave the `contextConfigLocation` Servlet parameter empty. diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.java index 2c52f38ad86..6f4a2fcb397 100644 --- a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.java +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.java @@ -18,15 +18,13 @@ package org.springframework.docs.web.webmvc.mvcservlet.mvccontainerconfig; import jakarta.servlet.Filter; import org.springframework.web.context.WebApplicationContext; -import org.springframework.web.context.support.AbstractDispatcherServletInitializer; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.filter.HiddenHttpMethodFilter; +import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer; // tag::snippet[] public class MyFilterDispatcherServletInitializer extends AbstractDispatcherServletInitializer { - // ... - @Override protected Filter[] getServletFilters() { return new Filter[] { @@ -36,17 +34,17 @@ public class MyFilterDispatcherServletInitializer extends AbstractDispatcherServ // @fold:on @Override protected WebApplicationContext createServletApplicationContext() { - return null; + /**/return null; } @Override protected String[] getServletMappings() { - return new String[] { "/" }; + /**/return new String[] { "/" }; } @Override protected WebApplicationContext createRootApplicationContext() { - return null; + /**/return null; } // @fold:off } diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.java new file mode 100644 index 00000000000..0259d184e44 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet.mvcservletcontexthierarchy; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +// tag::snippet[] +public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class[] getRootConfigClasses() { + return new Class[] { RootConfig.class }; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[] { App1Config.class }; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/app1/*" }; + } +} +// end::snippet[] + +class RootConfig {} +class App1Config {} diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.kt index 22042a211a7..1788c894d65 100644 --- a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.kt +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvccontainerconfig/MyFilterDispatcherServletInitializer.kt @@ -18,30 +18,28 @@ package org.springframework.docs.web.webmvc.mvcservlet.mvccontainerconfig import jakarta.servlet.Filter import org.springframework.web.context.WebApplicationContext -import org.springframework.web.context.support.AbstractDispatcherServletInitializer import org.springframework.web.filter.CharacterEncodingFilter import org.springframework.web.filter.HiddenHttpMethodFilter +import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer // tag::snippet[] class MyFilterDispatcherServletInitializer : AbstractDispatcherServletInitializer() { - // ... - override fun getServletFilters(): Array { return arrayOf(HiddenHttpMethodFilter(), CharacterEncodingFilter()) } // @fold:on override fun createServletApplicationContext(): WebApplicationContext { - TODO("Not yet implemented") + /**/TODO("Not yet implemented") } override fun getServletMappings(): Array { - return arrayOf("/") + /**/TODO("Not yet implemented") } override fun createRootApplicationContext(): WebApplicationContext? { - return null + /**/TODO("Not yet implemented") } // @fold:off } diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.kt new file mode 100644 index 00000000000..7ec1e771123 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet.mvcservletcontexthierarchy + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer + +// tag::snippet[] +class MyWebAppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() { + + override fun getRootConfigClasses(): Array> { + return arrayOf(RootConfig::class.java) + } + + override fun getServletConfigClasses(): Array> { + return arrayOf(App1Config::class.java) + } + + override fun getServletMappings(): Array { + return arrayOf("/app1/*") + } +} +// end::snippet[] + +class RootConfig +class App1Config diff --git a/framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.xml b/framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.xml new file mode 100644 index 00000000000..7db5049c3ef --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/web/webmvc/mvcservlet/mvcservletcontexthierarchy/MyWebAppInitializer.xml @@ -0,0 +1,29 @@ + + + + + org.springframework.web.context.ContextLoaderListener + + + + contextConfigLocation + /WEB-INF/root-context.xml + + + + app1 + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/app1-context.xml + + 1 + + + + app1 + /app1/* + + + +