2015-01-08 43 views
0

我正在開發一個基於Maven的Spring MVC中的Web應用程序,它使用eclipse juno進行休眠。 當我試圖在servlet-context.xml中配置hibernate時顯示如下錯誤:Spring MVC + Hibernate + Maven:在bean聲明中發現多個註釋

cvc-elt.1:找不到元素'beans'的聲明。

的servlet-context.xml中看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd"> 
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

<context:component-scan base-package="com.webworld.crm" /> 


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="${database.driver}" /> 
    <property name="url" value="${database.url}" /> 
    <property name="username" value="${database.user}" /> 
    <property name="password" value="${database.password}" /> 
</bean> 

<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"></property> 

    <property name="mappingResources"> 
    <list> 
    <value>employee.hbm.xml</value> 
    </list> 
    </property> 

    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">MySQL5Dialect</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
      <prop key="hibernate.show_sql">true</prop> 

     </props> 
    </property> 
</bean> 

<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <property name="sessionFactory" ref="mysessionFactory"></property> 
</bean> 

<bean id="d" class="com.javatpoint.EmployeeDao"> 
    <property name="template" ref="template"></property> 
</bean> 
</beans> 

文件結構如下:

enter image description here

請儘快回答這個問題。

+0

問題是你指定'彈簧mvc.xsd'作爲根名稱空間,名稱空間你可以使用沒有前綴。所以''對應於''。然而'spring-mvc.xsd'中''spring-beans.xsd'中沒有''標記。爲了使其工作,您需要將所有'',''等前綴加上'beans:',因爲這是您爲'spring-beans.xsd'指定的前綴。或者用'mvc'將根名稱空間切換爲'spring-beans.xsd'和前綴''。 –

+0

@ M.Deinum感謝您的時間,但如果您通過重寫代碼來詳細闡述這一點,那對我更有幫助。提前致謝。 –

回答

3
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/mvc" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:beans="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd"> 

問題是你指定spring-mvc.xsd爲根命名空間,你可以不帶前綴使用的命名空間。所以<annotation-driven />對應於<mvc:annotation-driven />。但是spring-mvc.xsd中沒有<bean>標籤,因爲它在spring-beans.xsd中。

爲了使它工作,你需要或者前綴所有<bean /><property />等與beans:,因爲這是你的spring-beans.xsd作爲前綴指定的。或者將根名稱空間切換爲spring-beans.xsd並將其前綴<annotation-driven />mvc

實施例與beans:前綴

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:beans="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd"> 


<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

實施例具有不同的根名稱空間

<?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:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd"> 

    <mvc:annotation-driven /> 

    <mvc:resources mapping="/resources/**" location="/resources/" /> 
+0

感謝您的時間,但是當我使用mvc前綴時,它顯示錯誤,如「前綴」mvc「元素」mvc:註釋驅動「不受限制」。我在你的答案中使用了代碼「帶有不同根名稱空間的例子」。 –

+0

糟糕忘記用'xmlns:mvc'替換'xmlns:beans'。答案已更新。 –

+0

此外,您擁有的問題基本上與Spring沒有任何關係,但僅僅是XML的工作原理。 –

0

對於第一個bean定義,您已經給出了** bean:** bean前綴,如下所示。

<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 

但是對於所有bean定義的其餘部分,您錯過了提供前綴「beans」的地方。 我認爲這可能是問題,因爲解析器無法識別此標記。請嘗試給所有標籤賦予前綴並嘗試。

+0

不,但我不想用這個老技術,我也更新我的問題。如果我使用推薦的代碼,它會在每個bean前面顯示錯誤。 –

+0

好的。嘗試對第一個標記中的xml模式定義進行重新排序,並將bean模式作爲默認值,並用其前綴進行休息。這裏的beans標籤是beans xml命名空間的一部分。 –

+0

現在請再次查看問題,現在錯誤已更改。 –