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

问题描述 投票:0回答:13

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

元素类型“web-app”的内容必须匹配 “(图标?,显示名称?,描述?,可分发?,上下文参数*,过滤器*,过滤器- 映射*、侦听器*、servlet*、servlet 映射*、会话配置?、mime 映射*、欢迎文件列表?、错误页面*、taglib*、资源环境- ref*、资源引用*、安全约束*、登录配置?、安全角色*、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。对这个问题有什么想法吗?

eclipse web.xml
13个回答
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>

22
投票

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


8
投票

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

因此,我开始从 ECLIPSE 的 XML 文件的设计视图添加每个元素。

它有效!您可以按照 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。


2
投票

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


2
投票

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

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

1
投票

如果您正在处理同样的问题并且发现 web.xml 语法没有任何问题,我建议执行以下操作:“剪切(web.xml 中的所有内容)”、“粘贴到记事本”-“从记事本复制” - “粘贴回 web.xml” - “最后保存 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>`

0
投票

我刚刚删除了 DOCTYPE 并重新启动了 Eclipse。有效。一定要尝试一下


0
投票

而不是使用“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”


0
投票

只需按照 web.xml 文件中的错误消息中所述订购标签即可。

这样订购:

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>ServletTest</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>pqr</servlet-name>
    <servlet-class>SqServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>abc</servlet-name>
    <url-pattern>/add</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>pqr</servlet-name>
    <url-pattern>/sq</url-pattern>
  </servlet-mapping>
</web-app>

不是这样的:

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>ServletTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>abc</servlet-name>
    <url-pattern>/add</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>pqr</servlet-name>
    <servlet-class>SqServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>pqr</servlet-name>
    <url-pattern>/sq</url-pattern>
  </servlet-mapping>
</web-app>
© www.soinside.com 2019 - 2024. All rights reserved.