元素类型“servlet-mapping”的内容必须匹配“(servlet-name,url-pattern)”

时间:2013-11-12 12:05:15

标签: web.xml url-pattern

我的文件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 id="WebApp_1383925467813">
    <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

   <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
        <url-pattern>/m/*</url-pattern>
        <url-pattern>/t/*</url-pattern>
    </servlet-mapping>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Myeclipse报告此错误:

  

元素类型“servlet-mapping”的内容必须匹配   “(servlet的名称,URL模式)”

有什么问题? 感谢的

2 个答案:

答案 0 :(得分:3)

请将您的DTD更改为3.0版本,该版本允许<url-pattern>内有多个<servlet-mapping>代码。

我刚刚将你的web.xml中的版本更改放在

下面
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
    version="3.0">
            <display-name>Archetype Created Web Application</display-name>
          <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
          </context-param>
          <listener>
            <listener-class>
                 org.springframework.web.context.ContextLoaderListener
            </listener-class>
          </listener>
         <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
          </servlet>

           <servlet-mapping>
                <servlet-name>dispatcher</servlet-name>
                <url-pattern>/</url-pattern>
                <url-pattern>/m/*</url-pattern>
                <url-pattern>/t/*</url-pattern>
            </servlet-mapping>


          <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
          </welcome-file-list>
        </web-app>

答案 1 :(得分:1)

根据DTD,我们在<url-pattern>代码中只能有一个<servlet-mapping>

<强> <!ELEMENT servlet-mapping (servlet-name, url-pattern)>

重写你的xml,如:

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/m/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/t/*</url-pattern>
</servlet-mapping>