0

Description:

  1. Set up a SpringMVC based Java EE project.
  2. run project, works well
  3. add Hibernate4 framework libraries support.

Problem,Exception

1)After I added 'dataSource' bean to SpringMVC configuration file, it'll always throw when start up.

Line 45 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'.

2)I referred to The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'

still the same error.

SpringMVC configuration XML file

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test" />
    <property name="username" value="root" />
    <property name="password" value="123edkx" />
</bean>

<mvc:resources mapping="/resources/**" location="/resources/" />
<!--HandlerMapping-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!--HandlerAdapter-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<!--ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/Views/" />
    <property name="suffix" value=".jsp" />
</bean>

<!--Controller -->
<bean class="com.annotation.controllers.Test" />
<bean class="com.annotation.controllers.LogicController" />

Questions:

anyone knows how to fix this? or advice some integration tutorial regarding SpringMVC3.x with Hibernate 4?

<?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:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                       http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                       http://www.springframework.org/schema/tx
                       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url" value="jdbc:sqlserver://localhost:1443;DatabaseName=beta_nl" />
    <property name="username" value="root" />
    <property name="password" value="12rekasQL" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!--<property name="packagesToScan" value="com.vo" />-->
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingLocations" value="classpath:com/vo/*.hbm.xml"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

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

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

<mvc:resources mapping="/resources/**" location="/resources/" />
<!--HandlerMapping-->
<!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!--HandlerAdapter-->
<!--<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<!--ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/Views/" />
    <property name="suffix" value=".jsp" />
</bean>

<!--Controller -->
<bean class="com.annotation.controllers.Test" />
<bean class="com.annotation.controllers.LogicController" />

Community
  • 1
  • 1
Saorikido
  • 2,173
  • 2
  • 26
  • 37

2 Answers2

0

I think it's that you didn't add the xsd to the beginning of config file. Below is the xsd and definition that you will use. xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd". and if you want to use tx annotation, you should also config a transaction manager.

OQJF
  • 1,350
  • 9
  • 12
  • and this http://stackoverflow.com/questions/8038830/different-spring-annotation-xml-declarations can help to understand the configurations of spring better. – OQJF Nov 21 '12 at 08:13
  • the xsd is there I didn't past out, and I also declared a transcation manager, – Saorikido Nov 21 '12 at 08:21
0

I've used http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/ recently and its configuration works just fine.

Please post the full configuration, with the namespace deсlarations and etc.

update: please check the presence of xsi namespace declaration

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • the full configuration has been update, but .. didn't appears I dont know why. – Saorikido Nov 21 '12 at 08:44
  • @user1837485 if you are using Eclipse consider switching to SpringSource Tool Suite - it has built-in tools to edit namespaces in Spring configurations. – Boris Treukhov Nov 21 '12 at 08:48
  • @user1837485 also you are missing `xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"` – Boris Treukhov Nov 21 '12 at 08:53
  • thank you for editing. actually I'm using intellij IEDA to created that project, is that possible I missed some jars caused that problem? that xml configurations is correct right? – Saorikido Nov 21 '12 at 08:55
  • @user1837485 do you have `xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"` in your xml configuration? – Boris Treukhov Nov 21 '12 at 08:55
  • then please add the *actual* file content with all the declarations to the question. :) – Boris Treukhov Nov 21 '12 at 09:03
  • sorry for that, just updated, that is the lastest one.thank you – Saorikido Nov 21 '12 at 09:15
  • if I only add id = 'dataSource' scope it occurs such kind of error either. – Saorikido Nov 21 '12 at 09:17
  • @user1837485 the file that you have posted seems to be correct, do you have any other error messages? P.S. don't forget to fix the typo in `localhost` – Boris Treukhov Nov 21 '12 at 09:29
  • are you using maven? what is your pom.xml? – Boris Treukhov Nov 21 '12 at 11:21
  • No, not use maven. do you have some simple code regarding 'integrate SpringMVC3 with Hibernate4' which running properly? – Saorikido Nov 22 '12 at 02:28
  • Hi Boris, good news, I think that exception is not the main exception which caused the application doesn't works properly, finally I found another excetiption 'driverClass can not be loaded',and I do added the property jar to the project already, but when I tried copy the driver jar to lib manually, it works! but still throws 'tx:annotation-driven' exception, I'm using glassfish as the container, tested tomcat it doesn't throw any excetion, unfortunately I still don't know what caused glassfish throw that exception. thank you. – Saorikido Nov 22 '12 at 07:11
  • Do you have anything except `Line 45 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven' in you console? – Boris Treukhov Nov 22 '12 at 07:15
  • Also please check that you are using the correct versions of Spring 3.1 .jar files – Boris Treukhov Nov 22 '12 at 07:31
  • Yes,that exception you mentioned is always there, but with that exception the code still work – Saorikido Nov 22 '12 at 10:38
  • Does your problem persist if you try to deploy the war file on another glassfish server instance? Also check for duplicate Spring jars http://stackoverflow.com/questions/3650252/problem-starting-spring-application-from-java – Boris Treukhov Nov 22 '12 at 10:48
  • just to rephrase, if everything works on tomcat then your problem is glassfish specific(maybe some stale data on the server?) – Boris Treukhov Nov 22 '12 at 11:27
  • I suppose you're right, I tried to release the war to glassfish on another computer manually, the exception's gone! It looks like some stale data caused this issue, I will try to reinstall glassfish then have a try, thank you – Saorikido Nov 23 '12 at 05:48