2015-05-06 118 views
0

當我試圖在瀏覽器上查看網頁時,我收到此錯誤。匹配通配符是嚴格的,但在調度程序servlet中沒有發現元素'context:component-scan'錯誤的聲明

堆棧跟蹤

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from ServletContext resource [/WEB-INF/DefaultServlet-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 71; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'. 

DefaultServlet-servlet.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 /> 
    <context:component-scan base-package="com.projectShaun.controller" /> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

我也將包括我的applicationContext,因爲它也有代碼在同一行:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 

    <tx:annotation-driven/> 

    <context:component-scan base-package="com.projectShaun.controller" /> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/projectshaun" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"></property> 
    <property name="annotatedClasses"> 
      <list> 
       <value>com.projectShaun.model.Account</value> 
      </list> 
     </property> 
    <property name="hibernateProperties"> 
     <props> 
     <prop 
     key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory"> 
    </bean> 
</beans> 

我也相信我有正確的依賴ency在我的圖書館和POM:

<dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
+0

回答弗拉基米爾·季霍米羅夫說,所有。 – Fabien

+0

你確實只有彈簧框架的這種依賴性嗎?我會假設你可能想要添加_spring-context-support_和_spring-beans_。 – Rufi

回答

1

根據這一post您還沒有指定的上下文命名空間的架構位置,就是這個特定的錯誤的原因」。

但是,看起來你有它,所以讓我們看看還有什麼可以的。

一種選擇可能是,你可以,因爲你在你的applicationContext 使用 3.2指定彈簧豆的版本,所以可能是有益的使用DefaultServlet-servlet.xml中同一個。

我感到擔心的另一件事是路徑http://www.springframework.org/schema/p,據我所知這個你不需要指定的schemaLocation。這似乎是正確的,但我沒有看到這麼多的優勢,加上一些XML命名空間沒有被使用。所以,我修改了它並得到了這個,嘗試一下,如果有幫助,希望它可以。從@

<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:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 

<tx:annotation-driven/> 

<context:component-scan base-package="com.projectShaun.controller" /> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/projectshaun" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="annotatedClasses"> 
     <list> 
      <value>com.projectShaun.model.Account</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

+0

OP具有上下文的模式位置。 – xyz

+0

我已經有計劃的位置不是嗎? – Shaun

+0

我編輯了答案,檢查它是否有幫助。 – Rufi

相關問題