Tomcat应用服务器启动时,会加载web.xml配置文件,然后通过其中的配置项来启动项目,只有配置的各项没有错误时,项目才能正常启动。
使用eclipse建立web项目时,eclipse会自动创建一个默认的web.xml文件,默认的web.xml文件只有<display-name>和<welcome-file-list>两个配置项,在此基础上我们可以加入更多的配置项。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>eshop</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
配置项<display-name>
定义web应用的名称,当服务器端用同一IP地址和同一端口(如80端口)部署多个web应用时,使用该名称区分不同的web应用。
配置项< welcome-file-list >
可以放置多个web应用首页文件名。当用户在浏览器输入web应用访问路径时,如果访问路径不包含明确的资源文件名称时,tomcat会根据该配置项首页文件名的放置顺序,在服务器目录中依次查找与文件名相匹配的物理文件,如果没有找到相匹配的物理文件,返回404错误。
本课程项目需要集成Spring框架,因此需要在web.xml配置文件中增加其它配置项。
配置项<servlet>
Servlet是服务端的程序,用于处理和响应客户端发送的请求。例如,当用户在浏览器输入表单信息并提交表单时,表单会对应一个服务端的Servlet程序处理该表单的请求。处理该表单的Servlet程序需要在配置项<servlet>中声明,tomcat才会把表单请求转发给该Servlet程序。
配置项<servlet>节点下面有<servlet-name>、<servlet-class>、<init-param>、<load-on-startup>子节点。
<servlet-name>子节点用于配置Servlet程序的名称。因为课程项目所有的servlet请求都会转发给Spring框架处理,因此<servlet-name>配置项的内容为spring即可。
<servlet-class>子节点用于配置Servlet程序的地址。该地址指向Spring框架的org.springframework.web.servlet.DispatcherServlet类。
<init-param>子节点用于配置Servlet程序初始化时需要传入的参数。传入的参数为Spring框架的spring-servlet.xml配置文件路径。
在Spring框架中,Spring默认存在两个配置文件:一个是applicationContext.xml配置文件,另一个是spring-servlet.xml配置文件。一般情况下, spring-servlet.xml配置文件会放在web.xml文件下的<servlet>配置项中加载,<init-param>子节点就是用于加载spring-servlet.xml配置文件的。
<load-on-startup>子节点用于配置是否在tomcat启动后自动加载Servlet程序,该子节点内容为空或负数时tomcat不会自动加载Servlet程序,只有Servlet被客户端请求时,tomcat才会加载该Servlet程序。如果该子节点的内容不为空并且大于或等于0(必须是整数),tomcat在启动时就会加载该Servlet程序,如果有多个Servlet程序,加载顺序由整数数值大小决定,整数的数值越小该servlet的优先级就越高,应用启动时就越先加载。
<servlet>配置项代码如下:
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:\config\spring_config\spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
在上面的代码中,<param-value>节点的内容是Spring框架spring-servlet.xml配置文件在项目中的文件路径,其中classpath指向项目编译后类文件的物理存放路径,默认是bulid/classes/。在下节课中,我们会在项目的src目录下建立congfig目录,在congfig目录下建立spring_config目录,用于存储Spring框架的配置文件。
配置项<servlet-mapping>
<servlet-mapping>配置项用于配置客户端请求映射路径,<servlet-mapping>配置项下有<servlet-name>、<url-pattern>两个子节点。
<servlet-name>子节点配置Servlet程序的名称,这里的名称应该和配置项<servlet>的名称一致。
<url-pattern>子节点指定相对于Servlet的url的路径,该路径相对于web应用程序上下文的根路径。url路径可以使用“*”通配符,例如,“*.do”表示所有以do为扩展名的请求都会发送到<servlet-name>指定的Servlet程序。
< servlet-mapping >配置项代码如下:
<servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
配置项< listener>
<listener>配置项用于配置监听类,用于监听某个事件的发生,或对象状态的改变。
<listener>配置项的子节点<listener-class>指定监听类,Spring框架的监听类是ContextLoaderListener。
< listener >配置项代码如下:
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
配置项< context-param>
用来声明整个WEB应用范围内的上下文初始化参数。该配置项下有<param-name>和<param-value>两个子节点。
<param-name>用于设定上下文的参数名称,必须是唯一名称;<param-value>用于设定参数名称的值。
tomcat启动时会读取<contex-param>配置项的内容,并将<param-name>和<param-value>节点的内容转化为键值对,交给ServletContext,ServletContext是整个WEB应用范围的上下文,Servlet程序可以通过ServletContext获取<context-param>的值进行初始化工作。
Spring框架的applicationContext.xml配置文件需要在< context-param>配置项中声明。代码如下:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:\config\spring_config\applicationContext.xml</param-value> </context-param>
在上面的代码中,<param-value>节点的内容是Spring框架applicationContext.xml配置文件在项目中的文件路径,其中classpath指向项目编译后类文件的物理存放路径,默认是bulid/classes/。在下节课中,我们会在项目的src目录下建立congfig目录,在congfig目录下建立spring_config目录,用于存储Spring框架的配置文件。
在<display-name>节点后面添加<absolute-ordering/>节点。由于ServletContext 3.0规范允许应用配置资源由多个配置文件(web.xml和web-fragment.xml)组成,这些配置文件会从一个应用中几个不同的位置被发现和加载,因此要设定加载顺序和避免重复加载问题的发生。<absolute-ordering>节点就是解决这个问题的,内容为空的<absolute-ordering>节点可以忽略掉所有的web-fragment配置文件。
完整的web.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>eshop</display-name> <absolute-ordering/> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:\config\spring_config\spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:\config\spring_config\applicationContext.xml</param-value> </context-param> </web-app>