web.xml文件解析

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"                      <=====表明此xml的元素满足此xsd的限制,为了了解各个元素的意义及子元素、属性,需要获取此xsd。

sun的已归属oracle,此xsd可以在http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html#7  找到,接下来用这些文档来一一解析各个元素。

    version="2.5">

    <display-name>Gradle + Spring MVC Hello World + XML</display-name>
    <description>Spring MVC web application</description>

    <!-- For web context -->
    <servlet>
        <servlet-name>hello-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>

    </servlet>


servlet元素的解析过程:

1、在web-app_3_1.xsd中未找到,servlet的定义,但是有如下语句:  <xsd:include schemaLocation="web-common_3_1.xsd"/>

定义可能在包含的xsd中

2、web-common_3_1.xsd中找到servlet的定义

<xsd:element name="servlet"
                   type="javaee:servletType"/>

<xsd:complexType name="servletType">
    <xsd:annotation>
      <xsd:documentation>

        The servletType is used to declare a servlet.
        It contains the declarative data of a
        servlet. If a jsp-file is specified and the load-on-startup
        element is present, then the JSP should be precompiled and
        loaded.
        
        Used in: web-app
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:group ref="javaee:descriptionGroup"/>
      <xsd:element name="servlet-name"
                   type="javaee:servlet-nameType"/>
      <xsd:choice minOccurs="0"
                  maxOccurs="1">
        <xsd:element name="servlet-class"
                     type="javaee:fully-qualified-classType">
          <xsd:annotation>
            <xsd:documentation>

              The servlet-class element contains the fully
              qualified class name of the servlet.
              
            </xsd:documentation>
          </xsd:annotation>
        </xsd:element>
        <xsd:element name="jsp-file"
                     type="javaee:jsp-fileType"/>
      </xsd:choice>
      <xsd:element name="init-param"
                   type="javaee:param-valueType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="load-on-startup"
                   type="javaee:load-on-startupType"
                   minOccurs="0">
        <xsd:annotation>
          <xsd:documentation>

            The load-on-startup element indicates that this
            servlet should be loaded (instantiated and have
            its init() called) on the startup of the web
            application. The optional contents of these
            element must be an integer indicating the order in
            which the servlet should be loaded. If the value
            is a negative integer, or the element is not
            present, the container is free to load the servlet
            whenever it chooses. If the value is a positive
            integer or 0, the container must load and
            initialize the servlet as the application is
            deployed. The container must guarantee that
            servlets marked with lower integers are loaded
            before servlets marked with higher integers. The
            container may choose the order of loading of
            servlets with the same load-on-start-up value.
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
      <xsd:element name="enabled"
                   type="javaee:true-falseType"
                   minOccurs="0"/>
      <xsd:element name="async-supported"
                   type="javaee:true-falseType"
                   minOccurs="0"/>
      <xsd:element name="run-as"
                   type="javaee:run-asType"
                   minOccurs="0"/>
      <xsd:element name="security-role-ref"
                   type="javaee:security-role-refType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="multipart-config"
                   type="javaee:multipart-configType"
                   minOccurs="0"
                   maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="id"
                   type="xsd:ID"/>
  </xsd:complexType>


  <xsd:complexType name="servlet-nameType">
    <xsd:annotation>
      <xsd:documentation>

        The servlet-name element contains the canonical name of the
        servlet. Each servlet name is unique within the web
        application.
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleContent>
      <xsd:extension base="javaee:nonEmptyStringType"/>
    </xsd:simpleContent>
  </xsd:complexType>



从上面的分析可以找到servlet-name是非空字符串,切每个servlet name必须是web app中唯一的。


<xsd:choice minOccurs="0"
                  maxOccurs="1">
        <xsd:element name="servlet-class"
                     type="javaee:fully-qualified-classType">
          <xsd:annotation>
            <xsd:documentation>

              The servlet-class element contains the fully
              qualified class name of the servlet.
              
            </xsd:documentation>
          </xsd:annotation>
        </xsd:element>
        <xsd:element name="jsp-file"
                     type="javaee:jsp-fileType"/>
      </xsd:choice>


  <xsd:complexType name="fully-qualified-classType">
    <xsd:annotation>
      <xsd:documentation>

        The elements that use this type designate the name of a
        Java class or interface.  The name is in the form of a
        "binary name", as defined in the JLS.  This is the form
        of name used in Class.forName().  Tools that need the
        canonical name (the name used in source code) will need
        to convert this binary name to the canonical name.
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleContent>
      <xsd:restriction base="javaee:string"/>
    </xsd:simpleContent>
  </xsd:complexType>


从上可以看出 class-name是字符串类型,用于定义servlet对应的类,使用完全限定的类名。

<xsd:complexType name="param-valueType">
    <xsd:annotation>
      <xsd:documentation>

        This type is a general type that can be used to declare
        parameter/value lists.
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:element name="description"
                   type="javaee:descriptionType"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
      <xsd:element name="param-name"
                   type="javaee:string">
        <xsd:annotation>
          <xsd:documentation>

            The param-name element contains the name of a
            parameter.
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
      <xsd:element name="param-value"
                   type="javaee:xsdStringType">
        <xsd:annotation>
          <xsd:documentation>

            The param-value element contains the value of a
            parameter.
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="id"
                   type="xsd:ID"/>
  </xsd:complexType>


可以看出init-param是一个参数名-值的序列,可以包含多组param-name param-value的

  <xsd:simpleType name="load-on-startupType">
    <xsd:union memberTypes="javaee:null-charType xsd:integer"/>
  </xsd:simpleType>

可以看出load-on-startup是一个空字符或者数字,用于表示是否随web app启动而启动


    <servlet-mapping>
        <servlet-name>hello-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

  <xsd:complexType name="servlet-mappingType">
    <xsd:annotation>
      <xsd:documentation>

        The servlet-mappingType defines a mapping between a
        servlet and a url pattern.
        
        Used in: web-app
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:element name="servlet-name"
                   type="javaee:servlet-nameType"/>
      <xsd:element name="url-pattern"
                   type="javaee:url-patternType"
                   minOccurs="1"
                   maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:attribute name="id"
                   type="xsd:ID"/>
  </xsd:complexType>


可以看出,servlet-mapping指定servlet与url的对应关系,可以有多组。


    <!-- For root context -->

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<xsd:complexType name="listenerType">
    <xsd:annotation>
      <xsd:documentation>

        The listenerType indicates the deployment properties for a web
        application listener bean.
        
      </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:group ref="javaee:descriptionGroup"/>
      <xsd:element name="listener-class"
                   type="javaee:fully-qualified-classType">
        <xsd:annotation>
          <xsd:documentation>

            The listener-class element declares a class in the
            application must be registered as a web
            application listener bean. The value is the fully
            qualified classname of the listener class.
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name="id"
                   type="xsd:ID"/>
  </xsd:complexType>


  指定的类必须注册未web app listener.


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-core-config.xml</param-value>
    </context-param>


<xsd:element name="context-param"
                   type="javaee:param-valueType">
        <xsd:annotation>
          <xsd:documentation>

            The context-param element contains the declaration
            of a web application's servlet context
            initialization parameters.
            
          </xsd:documentation>
        </xsd:annotation>
      </xsd:element>

可以看出context-param也是一个参数名-值对。

</web-app>


掌握了以上的学习路径,后续web.xml中如果遇到一些未接触过的,都可以从资料中自学。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值