【问题标题】:The content of element type "..." must match in web.xml元素类型“...”的内容必须在 web.xml 中匹配
【发布时间】:2012-09-10 10:52:47
【问题描述】:

我的 web.xml 文件有问题。错误:

元素类型“web-app”的内容必须匹配 "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter- mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env- ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)"。

但是,我的 web.xml 文件按照错误所说的顺序排列。

这是我的 web.xml:

<!DOCTYPE web-app PUBLIC
         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
         "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    </context-param>
      
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
        <description></description>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
      
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    <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>

我使用 WebLogic 10.3.4。对这个问题有任何想法吗?

【问题讨论】:

  • 是的,这个文件的顺序经常让我头疼。根据您发布的 DTD sn-p,welcome-file-list 应该出现在 servlet 和 servlet 映射之后。试试看。
  • 不,这不是问题。标签的顺序完全符合 DTD。
  • 确定吗? the DTD 声明 welcome-file-listservlet-mapping 之后 - 它位于以逗号分隔的元素列表中。
  • 是的,我确定。我改变了它。我还是有同样的问题。
  • 那么我很茫然 - 这是我唯一能看出它有问题的地方:/

标签: eclipse web.xml


【解决方案1】:

我在 Eclipse 中遇到了同样的问题,在将标签重新排序为 DTD 之后,错误就消失了。 您也可以尝试重新启动 Eclipse。

【讨论】:

  • re-order tags as DTD 是什么意思?
  • 重新排序标签意味着以 web.xml 指定的方式排列标签(如图标、显示名称、servlet、servlet-mapping)。当您将鼠标悬停在 eclipse 中的错误上时,您可以在帮助消息中看到顺序。
【解决方案2】:

像这样重新排列你的代码...

<web-app>
  <display-name>Archetype Created Web Application</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>

 <context-param>   
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
  </context-param>

  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
    <description></description>>
  </context-param>

  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 <servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>

如果您有更多 servlet,则在映射上方定义您的 servlet,然后再映射您的 servlet。

【讨论】:

    【解决方案3】:

    我按照某人的建议“复制所有”-“剪切”-“粘贴”-“保存”,这似乎清除了信息。比较之前和之后的文件,我发现在“粘贴”版本中,所有选项卡都已转换为空格。所以看来Eclipse中的web.xml验证器不喜欢tabs。

    【讨论】:

      【解决方案4】:

      如果您正在处理同样的问题并且发现 web.xml 语法没有任何问题,我建议您执行以下操作:“剪切(web.xml 中的所有内容)”、“粘贴到记事本”-“复制自记事本” - “粘贴回 web.xml” - “最后保存 web.xml”。一定要喜欢那些看不见的字符、标签等。

      【讨论】:

      • 这对我有用,想知道为什么:web.xml 上次更改是 16 个月,为什么突然出现 eclipse 错误?
      【解决方案5】:

      一个非常简单的解决方案可以解决我的问题。

      更改架构引用
      <!DOCTYPE web-app PUBLIC
         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
         "http://java.sun.com/dtd/web-app_2_3.dtd" >
      
      <web-app></web-app>
      

      到这里

      <?xml version="1.0" encoding="UTF-8"?>
      
      <web-app xmlns="http://java.sun.com/xml/ns/javaee" 
               version="2.5" 
               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"> 
               // ...
               // your all content goes here
      
      </web-app>
      

      【讨论】:

      • &lt;!DOCTYPE ... 行更改为&lt;?xml ... 对我来说已经足够了。
      【解决方案6】:

      我观察到 Web.xml 中的 DTD 需要元素 servlet、servlet-mapping 等的特定顺序。

      所以,我开始在 ECLIPSE 的 XML 文件的设计视图中添加每个元素。

      它有效!您可以按照 DTD 喜欢的方式构建 XML 文件。

      【讨论】:

        【解决方案7】:

        我刚刚删除了&lt;!DOCTYPE .. &gt; 标签,它对我有用。其实我不知道有多重要..

        【讨论】:

          【解决方案8】:

          最后,我通过在 Eclipse 中使用设计视图而不是直接在源视图中的 web.xml 中键入来配置 servlet 和 servlet 映射,从而解决了这个问题。希望这会有所帮助。

          【讨论】:

            【解决方案9】:

            此部分已删除,错误已解决。

            <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  "http://java.sun.com/dtd/web-app_2_3.dtd" >
            

            【讨论】:

              【解决方案10】:

              尝试上述解决方案时不要忘记保存文件。使用latest schema descriptor 并保存后,我的错误就消失了:

              <?xml version="1.0" encoding="UTF-8"?>
              <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
                  id="WebApp_ID" version="3.1">
              
                  <!-- your content here -->
              
              </web-app>`
              

              【讨论】:

                【解决方案11】:

                我刚刚删除了 DOCTYPE 并重新启动了 Eclipse。有效。试试看

                【讨论】:

                  【解决方案12】:

                  而不是使用“xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd""

                  使用

                  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

                  【讨论】:

                  • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案添加解释并说明适用的限制和假设。
                  猜你喜欢
                  • 2017-03-22
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-12-08
                  • 2015-06-26
                  • 2017-04-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多