ssm框架中,Java web项目的启动流程及web.xml配置文件

一、web.xml配置文件

项目启动与web.xml配置文件密不可分,web.xml配置文件用于初始化配置信息,包括welcome、context-param、listener、filter、filter-mapping、servlet、servlet-mapping、其他。如下

<?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_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<!--启动spring容器 -->
	<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>

	<!--springmvc的前端控制器,拦截所有请求 -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置springMVC需要加载的配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<!-- 匹配所有请求,此处也可以配置成 *.do 形式 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!--编码过滤器 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>forceResponseEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!--使用rest风格的url,将普通的post请求转为指定的delete或put请求 -->
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!--使ajax put请求有效  -->
	<filter>
		<filter-name>HttpPutFormContentFilter</filter-name>
		<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HttpPutFormContentFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app> 

 

 二、项目启动流程

概况图解

补充:

  • ContextLoaderListener初始化的spring容器中加载的bean共享于整个应用程序,如DAO层、Service层。
  • DispatcherServlet初始化的MVC上下文中加载的bean只对Spring Web MVC有效,如Controller层。

1.加载spring容器

(1)Tomcat启动一个web项目时,web容器会读取项目的配置文件web.xml,读取到<context-param>、<listener>两个标签。紧接着,web容器创建一个ServletContext对象(一个全局的servlet上下文,共享于整个项目)

(2)web容器将<context-param>标签中的内容转换为键值对,封装在ServletContext对象中。

(3)web容器创建<listener>中的类实例,创建监听器,如ContextLoaderListener监听器。

(4)ContextLoaderListener监听器在启动Web容器时,监听ServletContext对象的变化,获取ServletContext对象的<context-param>,按照applicationContext.xml的配置信息加载spring容器,自动装配组件(bean)。

<!--启动spring容器 -->
<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>

补充:

  • 在ContextLoaderListener 中关联了ContextLoader 这个类,所以整个加载配置过程由ContextLoader 来完成。
  • 监听器随web应用的启动而加载,只初始化一次,随web应用的停止而销毁。

 2.加载字符编码过滤器

<!--编码过滤器 -->
<filter>
	<filter-name>encodingFilter</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
	<init-param>
		<param-name>forceRequestEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
	<init-param>
		<param-name>forceResponseEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

3.加载springMVC的核心控制器

<!--springmvc的前端控制器,拦截所有请求 -->
<servlet>
	<servlet-name>SpringMVC</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 配置springMVC需要加载的配置文件 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>SpringMVC</servlet-name>
	<!-- 匹配所有请求,此处也可以配置成 *.do 形式 -->
	<url-pattern>/</url-pattern>
</servlet-mapping>

 补充:

  • DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点(也就是把前端请求分发到目标controller),而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
  • DispatcherServlet主要用作职责调度工作,本身主要用于控制流程
  • ssm项目运行流程:

(1)前端发送请求

(2)核心控制器DispatcherServlet调用请求解析器HandlerMapping对请求进行解析,通过映射关系匹配到Controller层的handler方法

(3)控制层调用其他层代码(如Service、DAO层),并将处理结果存在逻辑视图中

(4)核心控制器调用视图解析器解析逻辑视图,匹配到相应的页面文件,返回给前端。

4.其他 

猜你喜欢

转载自blog.csdn.net/qq_38861828/article/details/85096636