在Spring MVC程序中,配置Web.xml文件,要配置两个Servlet。web
第一个是org.springframework.web.context.ContextLoaderServlet
,用于在Web程序启动的时候,就加载一个配置文件,该配置文件的内容是为全部其web组件所共享的。无需接受请求即加载该文件,web程序启动的时候即加载了这个文件。示例以下:spring
<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
第二个是org.springframework.web.servlet.DispatcherServlet
,该Servlet的做用在于,处理各类web请求,只有当接受到请求,才会将这个DispatcherServlet所产生的上下文保存在Request中。示例以下:app
<!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>