First, web.xml file is the core configuration file of the project. Many configuration information is configurated, loaded and initialized through this file.
1. The loading order of the web.xml file is independent of its order in the web.xml file; the loading order is: ServletContext (context-param) - > listener - > filter - > servlet.
2. spring Core File: ApplicationContext. XML file. web.xml file loads applicationContext.xml
<context-param> <param-name>contextConfigLocation</param-name> <param- value>classpath*:**/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
3. The spring-mvc.xml file is the core file of spring MVC. web.xml loads spring-mvc.xml file
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet>
4. Strts2. XML file, web.xml load struts2.xml file;
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-BsfwtWeb-main.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>
Application Context.xml file function:
1. Configuration spring annotation, spring dependency injection, database related configuration, data source (if there are multiple databases, you can configure multiple data sources), configuration and spring-mybatis.xml file, configuration transaction and other information;
2. The application Context. XML file is located in the xx.properties file: if it is in the / WEB-INF / directory, the location is: / WEB-INF / file name. Suffix
<context:property-placeholder location="classpath:jdbc.properties"/>
3. spring-mvc.xml file:
1. When the web container starts up, read the web.xml configuration file, first go to the application Context.xml file, then spring-mvc file.
2. spring-mvc.xml file: start annotation (controller annotation), scan controller package annotation, static resource mapping, attempt to explain, file upload, return message json configuration;
4. Configuration of servlet s in web.xml files:
1. Write the corresponding server class.
2. Configuration in web.xml file and servlet information;
<servlet> <servlet-name>bondeServiceServlet</servlet-name> <servlet-class>cn.com.servyou.bonde.gate.entrance.EntranceServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <--! servlet Mapping, with the same name as above,url-pattern It is the suffix of visiting Lu Jin. --> <servlet-mapping> <servlet-name>bondeServiceServlet</servlet-name> <url-pattern>/bondegate/bondeServiceServlet</url-pattern> </servlet-mapping>
3. Access Strength: http://localhost/bondegate/bondeService Servlet
Fifth, filter (filter) in web.xml:
1. Define filter in web.xml:
<filter> <filter-name>EncodingFilterUtf-8</filter-name> <filter-class> cn.com.jdls.foundation.web.EncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilterUtf-8</filter-name> <url-pattern>*.ajax</url-pattern> </filter-mapping> <filter-mapping> <filter-name>EncodingFilterUtf-8</filter-name> <url-pattern>*.ado</url-pattern> </filter-mapping> <filter-mapping> <filter-name>EncodingFilterUtf-8</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>EncodingFilterUtf-8</filter-name> <url-pattern>/api/*</url-pattern> </filter-mapping>
2. Define the corresponding class: similar to the definition of servlet.
Summary: web.xml is the startup configuration file of the project (not required, but other configuration files are loaded through this file). If Struts2 framework is used, the struts2.xml configuration file is loaded by filter; spring MVC framework is loaded by servlet configuration; spring.xml file is loaded by line boot through configuration listener configuration; and finally mybatis.xml file is loaded by spring.xml file (in spring.xml). The mybatis configuration file is introduced into the file. At this point, the loading problem of web. XML file is over.