【问题标题】:should web.xml contents should be in order?web.xml 内容应该有序吗?
【发布时间】:2017-05-10 13:47:34
【问题描述】:

我当前的 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>contextConfigLocation</param-name>
<param-value>classpath:com/udemy/spring/config/security-context.xml</param-value>
</context-param>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-
class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

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

<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>dispatcherServlet</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
 </web-app>

但例如,如果我重新调整代码顺序,例如。像这样

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-
class>org.springframework.web.filter.DelegatingFilterProxy</filter-
class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/udemy/spring/config/security-context.xml</param-value>
</context-param>

我收到这个错误:-

The content of element type "web-app" must match "(icon?,display- name?,
description?,distributable?,context-param*,servlet*,servlet-
mapping*,session-config?,mime- mapping*,welcome-file-list?,error-
page*,taglib*,resource-ref*,security-constraint*,login-
config?,security- role*,env-entry*,ejb-ref*)".

谁能解释一下可能是什么原因?

【问题讨论】:

  • 第二个示例中的 web-app 标签在哪里?你在哪一行得到这些错误?

标签: java spring web.xml


【解决方案1】:

web.xml 元素的顺序在DTD 文件中定义,您在第一行中指定用于验证web.xml

http://java.sun.com/dtd/web-app_2_3.dtd

如果你打开这个文件,你会看到如下顺序:

  1. 图标
  2. 显示名称
  3. 说明
  4. ...等

正如在此元素中定义的那样:

<!ELEMENT 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*)>

【讨论】:

  • 很好的答案!虽然这在 servlet 3.1 dtd 中有所不同
【解决方案2】:

web.xml 文件通过 dtd 或 xml 模式进行验证。

在这个例子中http://java.sun.com/dtd/web-app_2_3.dtd

所以web.xml根据这个dtd严格定义了标签的顺序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多