Spring MVC framework detailed tutorial ﹣ DispatcherServlet and WebApplicationContext

DispatcherServlet

Dispatcher servlet is the core of the whole Spring MVC framework. It is responsible for distributing all requests to the controller. At the same time, it provides other functions needed for web application development. It integrates seamlessly with the Spring IoC container, which means that any features provided by Spring can be used in Spring MVC.

Dispatcher Servlet is a Servlet as its name suggests. So like Servlet, it needs to be configured in the web.xml file. The following code shows the configuration of dispatcher Servlet and the declaration of path mapping:

	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

The above code declares a dispatcher servlet named springdispatcher servlet. And set the start-up load with priority of 1 (the smaller the number, the higher the priority). All path requests under the project will be processed by the dispatcher servlet named springDispatcherServlet

  • <servlet-name>springDispatcherServlet</servlet-name>: the name of the configuration Servlet.

  • < servlet class > org. Springframework. Web. Servlet. Dispatcherservlet < / servlet class >: the class used by this servlet.

  • < load on startup > 1 < / load on startup >: start loading with priority of 1. The smaller the priority number, the higher the priority.

  • < URL pattern > / < URL pattern >: the path of the map.

Here is all the code for the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>SpringMVC</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- The front controller of this Spring Web application, responsible for 
		handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

Note: in addition to using web.xml to configure DispatcherServlet, we can also use java programming to configure DispatcherServlet(Servlet3.0 and above), but beginners should use web.xml to configure. Because it's simple!

WebApplicationContext

At this point, a dispatcher servlet is configured. However, it is not enough to only configure the dispatcher servlet, and other components (WebApplicationContext) need to be configured. The Web application context internally defines the beans needed by the project and generates the beans when the container starts. These beans include Controllers, ViewResolver, HandlerMapping, and so on.

WebApplicationContext inherits from ApplicationContext, which provides some features that web applications often need. It is different from ordinary ApplicationContext in that it supports topic parsing and knows which servlet it is associated with (it holds a reference to the ServletContext). WebApplicationContext is bound to ServletContext. If you need to get it, you can get the context WebApplicationContext of the web application through the static method in the RequestContextUtils tool class.

We have already configured the dispatcher Servlet in web.xml. At this time, DispatcherServlet will go to the / WEB-INF / path to find a file named [Servlet name] - Servlet.xml. Spring dispatcher Servlet is the name of the dispatcher Servlet we created, so the configuration file we are looking for is spring dispatcher Servlet-servlet.xml. springDispatcherServlet Servlet.xml is what we call WebApplicationContext above. Within it, the bean s needed by the project are defined.

/WEB-INF/springDispatcherServlet-servlet.xml is the default path for our configured DispatcherServlet to find configuration files. We can also modify web.xml to find the path of the configuration file.

	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
                <!-- Modify the default path of the profile -->
                <init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
        
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

Here we use a contextConfigLocation initialization parameter to specify the path of the configuration file. And it is configured in springmvc.xml under the classpath path path. Next let's create springmvc. XML.

Right click SRC - > New - > Spring bean configuration file (if you don't have this option, then your Eclipse doesn't contain Spring plug-ins. At this time, you can go to the Spring official website to download Spring Tools , or install the Spring plug-in for your Eclipse. It should be OK to create a normal xml file directly, but I haven't tried...)

Set the File name to springmvc.xml and click Finish.

The directory structure after creation is as follows:

Open springmvc.xml and click on the Namespaces under the edit area:

Select context:

And enter the following code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
    <!-- Scan annotation -->
	<context:component-scan base-package="com.pudding" />

</beans>

After the component scan is added, the annotation under the project will be scanned. If it is not added, @ Controller, @ Service and other labels will be invalid. Base package indicates which package to scan for annotations.

At this point, we created the dispatcher servlet and wrote the WebApplicationContext. The first step of spring MVC has been completed. We can write our first spring MVC project! There's no reason why it's not called HelloWorld

Tags: Java xml Spring JavaEE

Posted on Fri, 17 Apr 2020 10:17:49 -0700 by fean0r