Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC and Servlets 3.0 - Do you still need web.xml?

In a typical Spring MVC web app, you would declare the DispatcherServlet in web.xml like so

<!-- MVC Servlet --> <servlet>     <servlet-name>sample</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   <load-on-startup>1</load-on-startup> </servlet>  <servlet-mapping>     <servlet-name>sample</servlet-name>     <url-pattern>/</url-pattern> </servlet-mapping> 

Along with listeners, filters, etc.

With servlet-api 3.0, you can declare your servlets with the annotation @WebServlet instead of adding them to your web.xml. Spring 3.2 already has @Configuration and @EnableXYZ for its context configuration. Does it have something similar for the DispatcherServlet, ie. is there a way to configure your full Spring application without any xml?

like image 348
Sotirios Delimanolis Avatar asked Feb 21 '13 17:02

Sotirios Delimanolis


People also ask

Do We Need web xml in spring boot?

xml file is required! When required, however, we can take control over parts of the configuration and override the conventions that Spring Boot puts in play. We can also, if we really must, use traditional XML configuration files for some parts of the configuration.

Is web xml necessary?

Since the servlet 3 specification (IIRC) a web. xml is not needed anymore.

What is the role of web xml in Spring MVC?

Spring MVC web applications use the web. xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web.

When should I use web xml?

web. xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method. For example: the doGet() method for HTTP GET requests.


1 Answers

With JEE6, if your application container is Servlet 3.0 ready what you need to do is:

  1. Create a custom class that implements ServletContainerInitializer (i.e. com.foo.FooServletContainer)
  2. Create a file in your META-INF/services folder named javax.servlet.ServletContainerInitializer which will contain the name of your implementation above (com.foo.FooServletContainer)

Spring 3 is bundled with a class named SpringServletContainerInitializer that implements the stuff above (so you don't need to create yourself the file in META-INF/services. This class just calls an implementation of WebApplicationInitializer. So you just need to provide one class implementing it in your classpath (the following code is taken from the doc above).

public class FooInitializer implements WebApplicationInitializer {     @Override     public void onStartup(ServletContext servletContext) {         WebApplicationContext appContext = ...;          ServletRegistration.Dynamic dispatcher =            container.addServlet("dispatcher", new DispatcherServlet(appContext));         dispatcher.setLoadOnStartup(1);         dispatcher.addMapping("/");     }  } 

That's it for the web.xml thing, but you need to configure the webapp using @Configuration, @EnableWebMvc etc..

like image 135
Alex Avatar answered Sep 19 '22 22:09

Alex