What is the need of <mvc:default-servlet-handler />
in Spring MVC . When should we use it. When exactly is it needed. Why should we use it. I gone through few links in stackoverflow, but could not get clear picture or understanding. Can someone explain ?
The DispatcherServlet is the front controller in Spring web applications. It's used to create web applications and REST services in Spring MVC. In a traditional Spring web application, this servlet is defined in the web. xml file.
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.
In Spring MVC, web. xml used to be the place, where you needed to declare and configure Dispatcher Servlet, which is a Front Controller, receiving all the requests and dispatching to all the other components such as Controllers.
DispatcherServlet is the front controller in Spring Web MVC. Incoming requests for the HTML file are forwarded to the DispatcherServlet.
What is the need of <mvc:default-servlet-handler />
in Spring MVC
?
Using this handler
spring dispatcher will forward all requests to the default Servlet
. To enable the feature either you can use annotations or xml based configuration as below:
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
Or in XML:
<mvc:default-servlet-handler/>
What it will do?
The DefaultServletHttpRequestHandler
will attempt to auto-detect
the default Servlet
for the container
at startup time, using a list of known names for most of the major Servlet containers (including Tomcat, Jetty, GlassFish, JBoss, Resin, WebLogic, and WebSphere). If the default Servlet has been custom configured with a different name, or if a different Servlet container is being used where the default Servlet name is unknown, then the default Servlet’s name must be explicitly provided as in the following example:
@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable("myCustomDefaultServlet"); } }
Or in XML:
<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/>
When should we use it? When exactly is it needed? Why should we use it?
When you want spring dispatcher to serve static resources
under the web root
using default servlet.
If we are using DefaultServletHttpRequestHandler
, then we can replace :
<mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/images/**" location="/images/" />
with :
<mvc:default-servlet-handler />
More you can explore here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With