Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

When creating a Spring MVC project using the Spring Tool Suite 3 IDE, you can see, we have a web.xml file that defines the configuration for the DispatcherServlet, the root-context.xml and the servlet-context.xml files define bean configuration in Spring container including ViewResolver, a Controller handle request from the user and .jsp files to display information to the user. If you want to do all of this entirely with Java code, you can use the WebApplicationInitializer and WebMvcConfigurer interfaces. How is it in details? In this tutorial, I will guide you to configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer!

First, I will create a new Maven project:

Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

You can see this project is failing in the file pom.xml.

The reason is because I am declaring packaging in this file with the value “war” for web applications:

and the IDE is detecting that there is no web.xml file:

Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

We will not add web.xml file to fix this error. You can fix it using the Maven plugin maven-war-plugin and configure failOnMissingWebXml with false values as follows:

Now we will declare the dependencies needed for our Spring MVC project!

These dependencies are as follows:

To run this web application, I will use the Maven Jetty Plugin as follows:

OK, now we will go to the main part of this tutorial!

In a nutshell, the WebApplicationInitializer interface is used to define the configuration for the web application with the declaration for the DispatcherServlet and the Spring container. It used to replace that web.xml file!

The WebMvcConfigurer interface is used to declare Spring MVC related configurations like ViewResolver and beans needed for the application to run. We will also declare this class in the class implementing interface WebApplicationInitializer.

I will create a new class SpringConfiguration implement interface WebMvcConfigurer to define Spring container first.

You need to declare a Spring annotation of @EnableWebMvc to Spring import the default configuration from the WebMvcConfigurationSupport class to support Spring MVC. If you want to override any default configuration, you can override them in this SpringConfiguration class.

Here, I need to configure ViewResolver so I will use the addViewControllers() method to do this:

Here, I am declaring a view name as “index” mapping with request “/”. And as you can see, I also added a bean for the ViewResolver section using InternalResourceViewResolver. We will have the .jsp files in the /WEB-INF/views directory to build the View in the MVC model.

Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

The content of my index.jsp file is as follows:

Because we have declared the @Bean annotation in this class, you also need to add the @Configuration annotation for Spring to scan itself and create beans in its Spring container.

With the class WebApplicationInitializer, you can add a new class to implement it with the following content:

Here, we are using the AnnotationConfigWebApplicationContext class to configure how Spring will do to automatically create beans in its container using annotation. Spring will automatically scan the package “com.huongdanjava.springmvc” to do this.

We also initialize the DispatcherServlet and add it to the ServletContext. You can read this tutorial to understand how Spring will work with Java Servlet!

OK, now you can run our web application and go to http://localhost:8080 to check the results:

Mine is as follows:

Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

Add Comment