Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC configuration enable

I'm setting up a project from scratches, currently I'm on step of configuring Spring MVC 4.1.5 using java config. The whole app is beeing run on tomcat gradle plugin.

Can someone explain me why I need to make the following call to the class DefaultServletHandlerConfigurer in order to make requests map to my controllers ?

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

Without enabling it all my requests are beeing rejected and server says there is no mapping for particular request.

I read spring doc to find out, but the description doesn't tell me too much.

Enable forwarding to the "default" Servlet. When this method is used the DefaultServletHttpRequestHandler will try to auto-detect the "default" Servlet name. Alternatively, you can specify the name of the default Servlet via enable(String).

like image 530
ashur Avatar asked Apr 01 '15 15:04

ashur


People also ask

What is @configuration in Spring MVC?

Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. To enable autodetection of the annotated controllers, it is required to add component scanning to the configuration. It also gives the path of base package (i.e com.

What are the four parts that make up Spring MVC configuration?

@RequestMapping annotation maps web requests to Spring Controller methods. The terms model, view, and controller are as follows: Model: The Model encapsulates the application data. View: View renders the model data and also generates HTML output that the client's browser can interpret.

How do I enable Webmvc in Spring boot?

Annotation Type EnableWebMvc. Enables default Spring MVC configuration and registers Spring MVC infrastructure components expected by the DispatcherServlet . Use this annotation on an @ Configuration class. In turn that will import DelegatingWebMvcConfiguration , which provides default Spring MVC configuration.


1 Answers

It's explained very well in the Spring MVC section documentation here.

This allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container’s default Servlet), while still allowing static resource requests to be handled by the container’s default Servlet. It configures a DefaultServletHttpRequestHandler with a URL mapping of "/**" and the lowest priority relative to other URL mappings.

This handler will forward all requests to the default Servlet. Therefore it is important that it remains last in the order of all other URL HandlerMappings. That will be the case if you use or alternatively if you are setting up your own customized HandlerMapping instance be sure to set its order property to a value lower than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.

That piece of code is the equivalent to the xml line <mvc:default-servlet-handler/> in the Spring Web MVC-specific components file defined usually as servletname-servlet.xml

You have to use that call in order to configure the forwarding to the Spring default Servlet, if you don't make your server will try to use his own servlet handling and as you explained, if you don't have any specific defined it won't find any mapping for your requests.

like image 63
Leandro Carracedo Avatar answered Sep 25 '22 10:09

Leandro Carracedo