元素类型“...”的内容必须在web.xml中匹配。

44

我在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片段,欢迎文件列表应该出现在servlet和servlet映射之后。试试看吧。 - Faelkle
不,这不是问题。标签的顺序完全符合DTD。 - erencan
2
确定吗?DTD指定welcome-file-listservlet-mapping之后 - 它在元素的逗号分隔列表中稍后。 - Faelkle
是的,我确定。我已经更改了它。我仍然有同样的问题。 - erencan
可能是web app web.xml错误的重复问题。 - yatul
显示剩余2条评论
12个回答

83

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

将模式引用从

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

1
<!DOCTYPE ... 行更改为 <?xml ... 对我来说已经足够了。 - ROMANIA_engineer

22

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


2
你是什么意思说“重新按DTD排序标签”? - CodyBugstein
8
重新排序标签意味着按照web.xml文件规定的方式排列标签(例如icon、display-name、servlet、servlet-mapping)。当您将鼠标悬停在Eclipse中的错误上方时,可以在帮助消息中看到标签的顺序。请注意,不要更改原始含义,并使翻译易于理解。 - Rajkumar

8
我注意到Web.xml中DTD要求元素servlet、servlet-mapping等按特定顺序排列。因此,我从ECLIPSE的Design View中逐个添加每个元素。这样做是有效的!您可以按照DTD所需的方式构建XML文件。

7

我刚刚删除了<!DOCTYPE .. >标签,然后它就开始正常工作了。实际上,我不知道这个标签有多重要。


4

我按照某人的建议进行了“复制全部” - “剪切” - “粘贴” - “保存”,这似乎清除了消息。比较之前和之后的文件,我发现在“粘贴”的版本中,所有制表符都被转换为空格。因此,似乎Eclipse中的web.xml验证器不喜欢制表符。


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”是指Java编写的Web应用程序组件,用于处理客户端请求并生成响应。


2

已删除此部分并解决了错误。

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

2
最后,我通过在Eclipse的设计视图中配置Servlet和Servlet-mapping来解决了这个问题,而不是直接在web.xml的源视图中输入。希望这能有所帮助。

1

尝试上述解决方案时,请不要忘记保存文件。在我的情况下,使用最新模式描述符并保存后,错误消失了:

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

1
如果你遇到了同样的问题,而且在web.xml语法方面没有发现任何错误,我建议你按照以下步骤操作:“剪切(web.xml中的所有内容)”,“粘贴到记事本中” - “从记事本中复制” - “粘贴回web.xml” - “最后保存web.xml”。要爱那些看不见的字符、制表符等。

那对我有用,不知道为什么:web.xml 上一次更改是16个月前,所以为什么突然出现了eclipse错误? - timmacp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接