cvc-complex-type.2.4.c:匹配的通配符是严格的,但是对元素'tx:annotation-driven'没有声明 - Spring 4.3.7
问题描述:
我正在尝试配置Spring MVC + Hibernate,但是当我在我的servlet-context.xml文件中使用这个“tx:annotation-driven”,我在标题中得到错误。cvc-complex-type.2.4.c:匹配的通配符是严格的,但是对元素'tx:annotation-driven'没有声明 - Spring 4.3.7
我的servlet-context文件如下。 我使用Spring 4.3.7
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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
http://www.springframework.org/schema/tx/spring-tx-4.3.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="/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.nitin.SpringDemo.controller" />
<beans:bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<beans:property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<beans:property name="username" value="hr" />
<beans:property name="password" value="hr" />
</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.nitin.SpringDemo.controller</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="ServiceInterface" class="com.nitin.SpringDemo.controller.SpringDao">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<context:component-scan base-package="com.nitin.SpringDemo.controller" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
我是新的这个我想学习,但这种配置是服用大量的时间很沮丧,现在,请你都可以提供帮助。
答
xsi:schemaLocation
应该包含提示到XML文件处理器,在物理上获取将用于验证XML的xsd文件。这是充满
与URI引用的对(一个用于命名空间的名称,以及一个用于一个提示,一个架构文档定义了该命名空间指名道姓的位置)
引文来自https://www.w3.org/TR/xmlschema-1/#schema-loc
所以xsi:schemaLocation
应该包含对<schema-URI, physical-location-URI>
。在你的情况下,http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
是未配对的。
属性应该像
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"
(请注意,我插入http://www.springframework.org/schema/tx
作为倒数第二个组件)。
尝试在'schemaLocation'属性中插入'http:// www.springframework.org/schema/tx'。也就是说,用http://www.springframework.org/schema/tx http://www.springframework替换'http:// www.springframework.org/schema/tx/spring-tx-4.3.xsd'。 org/schema/tx/spring-tx-4.3.xsd' –
Heyy Roman它工作了!非常感谢 !但可以请你解释一下它的概念或逻辑...... –
太棒了!我已经发布了一些解释 –