简体   繁体   English

HTTP状态500-找不到元素'beans'的声明

[英]HTTP Status 500 - Cannot find the declaration of element 'beans'

I am trying write some simple controller. 我正在尝试编写一些简单的控制器。 And now I have one problem which I can't understand. 现在我有一个我无法理解的问题。 Please help me. 请帮我。

pom.xml: pom.xml中:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Diplom</groupId>
<artifactId>Diplom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>4.1.5.RELEASE</org.springframework-version>
    <org.aspectj-version>1.7.4</org.aspectj-version>
    <org.slf4j-version>1.7.5</org.slf4j-version>
    <hibernate.version>4.3.5.Final</hibernate.version>
    <mysql.version>5.1.29</mysql.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
<name>Diplom</name>
<description>Firth big project! :)</description>

web.xml : web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/myServlet-servlet.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>`

myServlet-servlet.xml: myServlet-servlet.xml中:

<beans xmlns="http:/www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans       
http://www.springframework.org/schema/beans/spring-beans.xsd      
http://www.springframework.org/schema/mvc      
http://www.springframework.org/schema/mvc/spring-mvc.xsd
  http://www.springframework.org/schema/context      
http://www.springframework.org/schema/context/spring-context.xsd ">
    <ctx:annotation-config>
    </ctx:annotation-config>
    <ctx:component-scan base-package="controllers">
    </ctx:component-scan>
</beans>

Text of Exeption: 证明文字:

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

I was tried delete "7 line" but then I had exception about "6 line" =D please help me to understand what is wrong 我曾尝试删除“ 7行”,但后来我对“ 6行” = D感到异常,请帮助我了解问题所在

You may need to add the spring-beans dependency to your maven. 您可能需要将spring-beans依赖项添加到您的Maven中。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.1.8.RELEASE</version>
</dependency>

You need to specify the version of Spring you are using in your servlet config - in your case spring-beans-4.1.xsd : 您需要在servlet配置中指定要使用的Spring版本-在这种情况下为spring-beans-4.1.xsd

xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
...

Can you please try with the below servlet xml? 您可以尝试使用以下servlet xml吗? In this I removed the closing tags, instead used them as <context:annotation-config /> and <context:component-scan /> . 在此,我删除了结束标记,而是将它们用作<context:annotation-config /><context:component-scan /> Also please provide your full package name in component-scan 另外,请在component-scan提供完整的软件包名称

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config />
    <context:component-scan base-package="controllers" />
</beans>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM