Provide support for filter registrations
The AbstractDispatcherServletInitializer now provides support for the registration of filters to be mapped to the DispatcherServlet. It also sets the asyncSupported flag by default on the DispatcherServlet and all registered filters. Issue: SPR-9696
This commit is contained in:
@@ -272,7 +272,33 @@
|
||||
<para>In the preceding example, all requests starting with
|
||||
<literal>/example</literal> will be handled by the
|
||||
<classname>DispatcherServlet</classname> instance named
|
||||
<literal>example</literal>. This is only the first step in setting up
|
||||
<literal>example</literal>.
|
||||
In a Servlet 3.0+ environment, you also have the
|
||||
option of configuring the Servlet container programmatically. Below is the code
|
||||
based equivalent of the above <filename>web.xml</filename> example:</para>
|
||||
|
||||
<programlisting language="java">public class MyWebApplicationInitializer implements WebApplicationInitializer {
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext container) {
|
||||
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
|
||||
registration.setLoadOnStartup(1);
|
||||
registration.addMapping("/example/*");
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
|
||||
<para><interfacename>WebApplicationInitializer</interfacename> is an interface
|
||||
provided by Spring MVC that ensures your code-based configuration is detected and
|
||||
automatically used to initialize any Servlet 3 container. An abstract base class
|
||||
implementation of this interace named
|
||||
<classname>AbstractDispatcherServletInitializer</classname> makes it even easier
|
||||
to register the <classname>DispatcherServlet</classname> by simply specifying
|
||||
its servlet mapping. See
|
||||
<link linkend="mvc-container-config">Code-based Servlet container initialization</link>
|
||||
for more details.</para>
|
||||
|
||||
<para>The above is only the first step in setting up
|
||||
Spring Web MVC. <!--The discussion below is a little vague about what you're doing, when you do it, and what you're accomplishing. --><!-- Is the next step shown in the next example screen?-->You
|
||||
now need to configure the various beans used by the Spring Web MVC
|
||||
framework (over and above the <classname>DispatcherServlet</classname>
|
||||
@@ -4394,6 +4420,108 @@ public class ErrorController {
|
||||
</filter-mapping></programlisting>
|
||||
</section>
|
||||
|
||||
<section id="mvc-container-config">
|
||||
<title>Code-based Servlet container initialization</title>
|
||||
|
||||
<para>In a Servlet 3.0+ environment, you have the option of configuring the
|
||||
Servlet container programmatically as an alternative or in combination with
|
||||
a <filename>web.xml</filename> file.
|
||||
Below is an example of registering a <classname>DispatcherServlet</classname>:</para>
|
||||
|
||||
<programlisting language="java">import org.springframework.web.WebApplicationInitializer;
|
||||
|
||||
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("/");
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
|
||||
<para><interfacename>WebApplicationInitializer</interfacename> is an interface
|
||||
provided by Spring MVC that ensures your implementation is detected and
|
||||
automatically used to initialize any Servlet 3 container. An abstract base
|
||||
class implementation of <interfacename>WebApplicationInitializer</interfacename> named
|
||||
<classname>AbstractDispatcherServletInitializer</classname> makes it even
|
||||
easier to register the <classname>DispatcherServlet</classname> by simply overriding
|
||||
methods to specify the servlet mapping and the location of the
|
||||
<classname>DispatcherServlet</classname> configuration:</para>
|
||||
|
||||
<programlisting language="java">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[] { "/" };
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
|
||||
<para>The above example is for an application that uses Java-based Spring
|
||||
configuration. If using XML-based Spring configuration, extend directly from
|
||||
<classname>AbstractDispatcherServletInitializer</classname>:</para>
|
||||
|
||||
<programlisting language="java">public class MyWebAppInitializer 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[] { "/" };
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
|
||||
<para><classname>AbstractDispatcherServletInitializer</classname> also provides
|
||||
a convenient way to add <interfacename>Filter</interfacename> instances
|
||||
and have them automatically mapped to the <classname>DispatcherServlet</classname>:</para>
|
||||
|
||||
<programlisting language="java">public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
|
||||
|
||||
// ...
|
||||
|
||||
@Override
|
||||
protected Filter[] getServletFilters() {
|
||||
return new Filter[] { new HiddenHttpMethodFilter(), new CharacterEncodingFilter() };
|
||||
}
|
||||
|
||||
}</programlisting>
|
||||
|
||||
<para>Each filter is added with a default name based on its concrete type and
|
||||
automatically mapped to the <classname>DispatcherServlet</classname>.</para>
|
||||
|
||||
<para>The <code>isAsyncSupported</code> protected method of
|
||||
<classname>AbstractDispatcherServletInitializer</classname> provides
|
||||
a single place to enable async support on the <classname>DispatcherServlet</classname>
|
||||
and all filters mapped to it. By default this flag is set to <code>true</code>.</para>
|
||||
|
||||
</section>
|
||||
|
||||
<section id="mvc-config">
|
||||
<title>Configuring Spring MVC</title>
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
</section>
|
||||
|
||||
<section id="new-in-3.2-webmvc-controller-advice">
|
||||
<title>New <interfacename>@ControllerAdvice</interfacename> annotation</title>
|
||||
<title><interfacename>@ControllerAdvice</interfacename> annotation</title>
|
||||
|
||||
<para>Classes annotated with <interfacename>@ControllerAdvice</interfacename>
|
||||
can contain <interfacename>@ExceptionHandler</interfacename>,
|
||||
@@ -88,8 +88,21 @@
|
||||
For more details see <xref linkend="mvc-ann-matrix-variables"/>.</para>
|
||||
</section>
|
||||
|
||||
<section id="new-in-3.2-dispatcher-servlet-initializer">
|
||||
<title>Abstract base class for code-based Servlet 3+ container initialization</title>
|
||||
|
||||
<para>An abstract base class implementation of the
|
||||
<interfacename>WebApplicationInitializer</interfacename> interface is provided to
|
||||
simplify code-based registration of a DispatcherServlet and filters
|
||||
mapped to it. The new class is named
|
||||
<classname>AbstractDispatcherServletInitializer</classname> and its
|
||||
sub-class <classname>AbstractAnnotationConfigDispatcherServletInitializer</classname>
|
||||
can be used with Java-based Spring configuration.
|
||||
For more details see <xref linkend="mvc-container-config"/>.</para>
|
||||
</section>
|
||||
|
||||
<section id="new-in-3.2-webmvc-exception-handler-support">
|
||||
<title>New <classname>ResponseEntityExceptionHandler</classname> class</title>
|
||||
<title><classname>ResponseEntityExceptionHandler</classname> class</title>
|
||||
|
||||
<para>A convenient base class with an
|
||||
<interfacename>@ExceptionHandler</interfacename>
|
||||
|
||||
Reference in New Issue
Block a user