Cvc-elt.1:无法找到元素' bean的声明。的servlet

时间:2017-08-04 16:03:15

标签: java spring servlets

我在简单的项目上遇到了问题。我使用的是Spring Tool Suite 3.8.3。你可以帮帮我吗? 有短版本的错误。

  

HTTP状态[500] - [内部服务器错误]

     

Servlet [DispatcherServlet]的消息Servlet.init()引发了异常

     

描述服务器遇到意外情况,导致无法完成请求

     

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自ServletContext资源[/WEB-INF/spring/webcontext/DispatcherServlet-context.xml]的XML文档中的第11行无效;嵌套异常是org.xml.sax.SAXParseException; lineNumber:11; columnNumber:64; cvc-elt.1:无法找到元素'

的声明      

cvc-elt.1:找不到元素' bean的声明。

的DispatcherServlet-context.xml中



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	<mvc:annotation-driven enable-matrix-variables="true" />
	<context:annotation-config />
	<context:component-scan base-package="com.kk.gamestore" />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="messages" />
	</bean>
</beans>
&#13;
&#13;
&#13;

Web.xml中

&#13;
&#13;
<web-app version="3.0" 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">
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/webcontext/DispatcherServlet-context.xml</param-value>
		</init-param>
		    <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>
&#13;
&#13;
&#13;

的pom.xml

&#13;
&#13;
<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>com.kk</groupId>
  <artifactId>gamestore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  		<version>4.0.3.RELEASE</version>
  	</dependency>
  	<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
   <version>3.1.0.RELEASE</version>
</dependency>
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>jstl</artifactId>
  		<version>1.2</version>
  	</dependency>
  	<dependency>
  		<groupId>javax.servlet</groupId>
  		<artifactId>javax.servlet-api</artifactId>
  		<version>3.1.0</version>
  		<scope>provided</scope>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-config</artifactId>
  		<version>3.1.4.RELEASE</version>
  		<exclusions>
  			<exclusion>
  				<groupId>org.springframework</groupId>
  				<artifactId>spring-asm</artifactId>
  			</exclusion>
  		</exclusions>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.security</groupId>
  		<artifactId>spring-security-web</artifactId>
  		<version>3.1.4.RELEASE</version>
  	</dependency>
  </dependencies>
  <properties>
  	<maven.compiler.source>1.7</maven.compiler.source>
  	<maven.compiler.target>1.7</maven.compiler.target>
  </properties>
</project>
&#13;
&#13;
&#13;

安全context.xml中

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
		
	<security:http auto-config="true">
		<security:intercept-url pattern="/games/add" access="ROLE_ADMIN" />
		
		<security:form-login login-page="/login" 
               default-target-url="/games/add"   
               authentication-failure-url="/loginfailed"/>
        <security:logout logout-success-url="/logout" />
	</security:http>
	
	<security:authentication-manager>
		<security:authentication-provider>
			<security:user-service>
				<security:user name="Admin" password="Admin123" authorities="ROLE_ADMIN" />
			</security:user-service>
		</security:authentication-provider>
	</security:authentication-manager>
</beans>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您必须使用相同的Spring Framework版本。

你已经拥有:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.0.RELEASE</version>
    </dependency>

因此4.0.3.RELEASE3.1.0.RELEASE 等于。选择其中一个,但要注意哪个版本,因为Spring Framework必须匹配Spring Security所需的版本。

如果Spring Security需要3.1.4.RELEASE 4.0.x或3.1.x系列

,请查看Spring Framework的文档

此外,对于所有 Spring .xsd 引用,请删除该版本。例如

发件人:http://www.springframework.org/schema/context/spring-context-3.2.xsd"收件人:http://www.springframework.org/schema/context/spring-context.xsd"(删除 -3.2

如果没有该版本,Spring可以根据.xsd文件中定义的dependency选择较高的pom.xml版本。 .xsd文件当然位于罐子中。