您的位置:首页 > 其它

web.xml文件解析

2015-11-15 13:53 316 查看
<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>

     
4000
   <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中如果遇到一些未接触过的,都可以从资料中自学。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: