We will create a class called SpringApplicationConfig, where we will register all Spring-related beans using Spring's Java-based configuration style. Let's see the code below.
As you can see, in this class, we will use annotation @ComponentScanand the input class path for packages that we want Spring Framework to scan. This annotation is like we are using tag <context:component-scan base-package="....." /> in Spring MVC XML Configuration.
Next, we also need to use annotation @EnableWebMvc to Enable Spring MVC-specific annotations. This annotation is like we are using tag <mvc:annotation-driven/> in Spring MVC XML Configuration.
Finally, we will create a viewResolver bean which will be used for Spring MVC View Resolver.
In which, we will create a new instance of InternalResourceViewResolver then we will configure viewClass to set the view class to be used by the view resolver to JstlView, which is a Spring view class that provides support for JSTL (JavaServer Pages Standard Tag Library).
The prefix is set to "/WEB-INF/views/" and the suffix to ".jsp", which means that when a view name is returned by a controller method, Spring will look for a JSP file with that name in the "/WEB-INF/views/" directory with a ".jsp" extension.
Add Java Configuration Class For Configuring Dispatcher Servlet#
Let's create a Java class that implements the WebApplicationInitializer interface as below which is used to configure a web application in a Servlet 3.0+ container. This class will replace our traditional web.xml.
As you can see we will override the method onStartup, this method is called by the container when the application starts up.
Then we will create a new AnnotationConfigWebApplicationContext for creating and configuring Spring web application Context using Java-based configuration as in the image below.
Next, the application context is then set to the servlet context using the setServletContext() method.
Then we will create a new DispatcherServlet with the application context as a param.
Then we will create an instance of ServletRegistration.Dynamic by using servletContext.addServlet() and the DispatcherServlet will be added into a dynamic servlet registration with the name is dispatcher and it maps to the root URL (/).
Finally, the servlet's loadOnStartup property is set to 1, indicating that the servlet should be loaded on application startup.
Firstly, we would like to package our project to .war file by using tag packaging instead of .jar (default) because we will use this .war file to deploy to Tomcat Server.
Next, we will use the dependency spring-webmvc to use Spring MVC framework.
Finally, we will use the plugin below, for supporting building .war file, we also have to configure path to the file web.xml that we are going to create in the next steps. With this plugin, our .war file will be packaged will all references and supported libraries, classes and configuration files.
We will create a class called SpringApplicationConfig, where we will register all Spring-related beans using Spring's Java-based configuration style. Let's see the code below.
We will use annotation @ComponentScanand the input class path for packages that we want Spring Framework to scan. Then, we also need to use annotation @EnableWebMvc to Enable Spring MVC-specific annotations.
Finally, we will create a viewResolver bean which will be used for Spring MVC View Resolver.
Add Java Configuration Class For Configuring Dispatcher Servlet#
Let's create a Java class that implements the WebApplicationInitializer interface as below which is used to configure a web application in a Servlet 3.0+ container. This class will replace our traditional web.xml.
So, let's create the folder views inside the WEB-INF as below.
Then inside the views we will create an example welcome.jsp with the content as below.
welcome.jsp
1 2 3 4 5 6 7 8 9101112
<%@pagelanguage="java"contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>FirstJSP</title></head><body><h3>Welcome${name}</h3><br></body></html>
In this welcome.jsp file, we just simply map the value of variable name that we will define in the Controller.
Next, let's create a controller class in side the parent package spring.mvc.java.based.configuration.controller that we configured in the SpringApplicationConfig.java class.
The created controller WelcomeController will have the content as below.
We can annotate classic controllers with the @Controller annotation. This is simply a specialization of the @Component class, which allows us to auto-detect implementation classes through the classpath scanning.
We typically use @Controller in combination with a @RequestMapping annotation for request handling methods.
In the api /welcome, we just simply add an attribute name with value Duc to the welcome.jsp view, the return "welcome"; means we will use the file welcome.jsp in the views folder that we configured and the attribute name will be mapped with value Duc there.
Finally, let's deploy this .war file into the Tomcat server and then go to the browser and put the url http://localhost:8080/spring-mvc-xml-configuration/welcome to check the successful result.