【问题标题】:Exception rising while running spring mvc-hibernate application运行spring mvc-hibernate应用程序时出现异常
【发布时间】:2021-05-29 13:49:31
【问题描述】:

我收到以下异常

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

我的servlet-context.xml如下,

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

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

          <!-- Hibernate 4 SessionFactory Bean definition -->
         <beans:bean id="hibernate4AnnotatedSessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
           <beans:property name="dataSource" ref="dataSource" />
           <beans:property name="annotatedClasses">
            <beans:list>
            <beans:value>com.srinath.model.Student</beans:value>
            </beans:list>
           </beans:property>
           <beans:property name="hibernateProperties">
            <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
             </beans:props>
             </beans:property>
              </beans:bean>

              <beans:bean id="studentDao" class="com.srinath.dao.StudentDaoImpl">
                <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
                </beans:bean>
               <beans:bean id="studentService" class="com.srinath.service.StudentServiceImpl">
                <beans:property name="studentDao" ref="studentDao"></beans:property>
                </beans:bean>
                  <context:component-scan base-package="com.srinath.controllers" />

                 <tx:annotation-driven transaction-manager="transactionManager"/>

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

                   </beans:beans>

我已经拿了spring mvc和hibernate应用的集成代码。我根据我的要求修改了一些代码。运行代码时出现上述异常。有什么解决办法吗。我拿的代码运行良好。但无法运行修改后的代码。

【问题讨论】:

    标签: java xml spring hibernate spring-mvc


    【解决方案1】:

    我想问题可能出在以httpshttp 开头的命名空间。尝试在根元素中使用以下命名空间声明:

    <beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns="http://www.springframework.org/schema/mvc"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
        https://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context 
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        https://www.springframework.org/schema/tx/spring-tx.xsd">
    

    【讨论】:

    • 我不知道命名空间,它们是如何工作的。如果你有教程链接,请发布。
    猜你喜欢
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    相关资源
    最近更新 更多