Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need and use of <mvc:default-servlet-handler />

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 ?

like image 294
Avinash Reddy Avatar asked Jul 10 '15 16:07

Avinash Reddy


People also ask

What is dispatcher servlet in Spring MVC?

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.

What is MVC in Spring framework?

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.

What is the use of web xml in Spring MVC?

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.

What is front controller in Spring MVC?

DispatcherServlet is the front controller in Spring Web MVC. Incoming requests for the HTML file are forwarded to the DispatcherServlet.


1 Answers

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.

like image 173
Arpit Aggarwal Avatar answered Sep 22 '22 18:09

Arpit Aggarwal