9 changed files with 342 additions and 169 deletions
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
/* |
||||
* 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.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; |
||||
|
||||
// tag::snippet[]
|
||||
public class MyFilterDispatcherServletInitializer extends AbstractDispatcherServletInitializer { |
||||
|
||||
// ...
|
||||
|
||||
@Override |
||||
protected Filter[] getServletFilters() { |
||||
return new Filter[] { |
||||
new HiddenHttpMethodFilter(), new CharacterEncodingFilter() }; |
||||
} |
||||
|
||||
// @fold:on
|
||||
@Override |
||||
protected WebApplicationContext createServletApplicationContext() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
protected String[] getServletMappings() { |
||||
return new String[] { "/" }; |
||||
} |
||||
|
||||
@Override |
||||
protected WebApplicationContext createRootApplicationContext() { |
||||
return null; |
||||
} |
||||
// @fold:off
|
||||
} |
||||
// end::snippet[]
|
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* 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.mvccontainerconfig; |
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; |
||||
|
||||
// tag::snippet[]
|
||||
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { |
||||
|
||||
@Override |
||||
protected Class<?>[] getRootConfigClasses() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
protected Class<?>[] getServletConfigClasses() { |
||||
return new Class<?>[] { MyWebConfig.class }; |
||||
} |
||||
|
||||
@Override |
||||
protected String[] getServletMappings() { |
||||
return new String[] { "/" }; |
||||
} |
||||
} |
||||
// end::snippet[]
|
||||
|
||||
class MyWebConfig {} |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* |
||||
* 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.mvccontainerconfig; |
||||
|
||||
import jakarta.servlet.ServletContext; |
||||
import jakarta.servlet.ServletRegistration; |
||||
import org.springframework.web.WebApplicationInitializer; |
||||
import org.springframework.web.context.support.XmlWebApplicationContext; |
||||
import org.springframework.web.servlet.DispatcherServlet; |
||||
|
||||
// tag::snippet[]
|
||||
public class MyWebApplicationInitializer implements WebApplicationInitializer { |
||||
|
||||
@Override |
||||
public void onStartup(ServletContext container) { |
||||
XmlWebApplicationContext appContext = new XmlWebApplicationContext(); |
||||
appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); |
||||
|
||||
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext)); |
||||
registration.setLoadOnStartup(1); |
||||
registration.addMapping("/"); |
||||
} |
||||
} |
||||
// end::snippet[]
|
||||
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
/* |
||||
* 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.mvccontainerconfig; |
||||
|
||||
import org.springframework.web.context.WebApplicationContext; |
||||
import org.springframework.web.context.support.AbstractDispatcherServletInitializer; |
||||
import org.springframework.web.context.support.XmlWebApplicationContext; |
||||
|
||||
// tag::snippet[]
|
||||
public class MyXmlDispatcherServletInitializer extends AbstractDispatcherServletInitializer { |
||||
|
||||
@Override |
||||
protected WebApplicationContext createRootApplicationContext() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
protected WebApplicationContext createServletApplicationContext() { |
||||
XmlWebApplicationContext cxt = new XmlWebApplicationContext(); |
||||
cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); |
||||
return cxt; |
||||
} |
||||
|
||||
@Override |
||||
protected String[] getServletMappings() { |
||||
return new String[] { "/" }; |
||||
} |
||||
} |
||||
// end::snippet[]
|
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* 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.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 |
||||
|
||||
// tag::snippet[] |
||||
class MyFilterDispatcherServletInitializer : AbstractDispatcherServletInitializer() { |
||||
|
||||
// ... |
||||
|
||||
override fun getServletFilters(): Array<Filter> { |
||||
return arrayOf(HiddenHttpMethodFilter(), CharacterEncodingFilter()) |
||||
} |
||||
|
||||
// @fold:on |
||||
override fun createServletApplicationContext(): WebApplicationContext { |
||||
TODO("Not yet implemented") |
||||
} |
||||
|
||||
override fun getServletMappings(): Array<String> { |
||||
return arrayOf("/") |
||||
} |
||||
|
||||
override fun createRootApplicationContext(): WebApplicationContext? { |
||||
return null |
||||
} |
||||
// @fold:off |
||||
} |
||||
// end::snippet[] |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* |
||||
* 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.mvccontainerconfig |
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer |
||||
|
||||
// tag::snippet[] |
||||
class MyWebAppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() { |
||||
|
||||
override fun getRootConfigClasses(): Array<Class<*>>? { |
||||
return null |
||||
} |
||||
|
||||
override fun getServletConfigClasses(): Array<Class<*>>? { |
||||
return arrayOf(MyWebConfig::class.java) |
||||
} |
||||
|
||||
override fun getServletMappings(): Array<String> { |
||||
return arrayOf("/") |
||||
} |
||||
} |
||||
// end::snippet[] |
||||
|
||||
class MyWebConfig |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* 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.mvccontainerconfig |
||||
|
||||
import jakarta.servlet.ServletContext |
||||
import org.springframework.web.WebApplicationInitializer |
||||
import org.springframework.web.context.support.XmlWebApplicationContext |
||||
import org.springframework.web.servlet.DispatcherServlet |
||||
|
||||
// tag::snippet[] |
||||
class MyWebApplicationInitializer : WebApplicationInitializer { |
||||
|
||||
override fun onStartup(container: ServletContext) { |
||||
val appContext = XmlWebApplicationContext() |
||||
appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml") |
||||
|
||||
val registration = container.addServlet("dispatcher", DispatcherServlet(appContext)) |
||||
registration.setLoadOnStartup(1) |
||||
registration.addMapping("/") |
||||
} |
||||
} |
||||
// end::snippet[] |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
/* |
||||
* 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.mvccontainerconfig |
||||
|
||||
import org.springframework.web.context.WebApplicationContext |
||||
import org.springframework.web.context.support.AbstractDispatcherServletInitializer |
||||
import org.springframework.web.context.support.XmlWebApplicationContext |
||||
|
||||
// tag::snippet[] |
||||
class MyXmlDispatcherServletInitializer : AbstractDispatcherServletInitializer() { |
||||
|
||||
override fun createRootApplicationContext(): WebApplicationContext? { |
||||
return null |
||||
} |
||||
|
||||
override fun createServletApplicationContext(): WebApplicationContext { |
||||
return XmlWebApplicationContext().apply { |
||||
setConfigLocation("/WEB-INF/spring/dispatcher-config.xml") |
||||
} |
||||
} |
||||
|
||||
override fun getServletMappings(): Array<String> { |
||||
return arrayOf("/") |
||||
} |
||||
} |
||||
// end::snippet[] |
||||
Loading…
Reference in new issue