1

在 apache tomcat 上运行我的应用程序时出现以下错误。

HTTP Status 500 - Servlet.init() for servlet spring threw exception

根本原因

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 
Line 11 in XML document from ServletContext resource [/WEB-INF/spring-servlet.xml] is invalid;
 nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.

我的 spring-context.xml 是这个

            <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:cotext="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:beans="http://www.springframework.org/schema/beans" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
                         http://www.springframework.org/schema/mvc">

     <mvc:annotation-driven/>
     <cotext:component-scan base-package="springmvc"/>   

我试过https://jira.springsource.org/browse/SPR-3372没有运气。

请帮忙。

4

2 回答 2

1

在我看来,几件事:

1)您必须删除 xmlns 属性中的“ http://java.sun.com/xml/ns/javaee ”。我认为这是这里的主要问题。根据规范,“xmlns”属性不能采用多个 URI:http: //www.w3schools.com/xml/xml_namespaces.asp

2)一些XML库不喜欢XML文件前面的空格(特别是Eclipse会抱怨)

3) xsi:schemaLocation 值缺少 mvc 的位置 URI。它应该看起来像这样:

 xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/context/spring-mvc-3.0.xsd">

您也可以像之前的答案所建议的那样删除额外的“beans”声明,除非您出于某种原因真的想使用“beans”命名空间前缀。

于 2012-12-02T01:32:12.017 回答
0

xmlns:beans="http://www.springframework.org/schema/beans"您在 XML 标头中有重复输入。删除它的一个实例并尝试如下:

<beans xmlns="http://www.springframework.org/schema/beans http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:cotext="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/mvc">

      <mvc:annotation-driven/>
      <cotext:component-scan base-package="springmvc"/>   
于 2012-12-02T00:48:39.877 回答