26

Currently Running

Tomcat: v6

Spring Tools Suite: v2.7.2

Spring Framework: spring-webmvc-3.0.5

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

      <mvc:annotation-driven />

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

      <context:component-scan base-package="com.app.mvc" />

 </beans>

web.xml partial code

<servlet-mapping>
    <servlet-name>duckapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Servlet Purpose

web.xml maps all urls to the servlet with the exception of mvc:resources mapping static files.

Bugs

  • cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'. app-servlet.xml /app/www/WEB-INF

  • cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'. app-servlet.xml /app/www/WEB-INF

Known Issues

Question

How can I fix the compile errors to get mvc:resources working correctly?

I've been digging around 2 hours for this, no solid answer yet...

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
Luc Laverdure
  • 1,398
  • 2
  • 19
  • 36
  • I don't think it is desirable to use version in xsd declaration. Spring will use highest version in the project's dependencies if version not given. – Aniket Thakur Dec 26 '15 at 16:12

5 Answers5

46

In your spring context xml mvc namespace url should match url in schemaLocation. Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

This is a standard XML namespace declaration. The namespace url is sort of an unique id, which is then mapped to the actual schema location in xsi:schemaLocation.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • Thanks, worked for me too. I wish this was better documented in Spring – Joseph Lust Sep 16 '11 at 17:45
  • 1
    Doesnt work. i have the exact same problem but using spring 3.1 – Jono Feb 09 '12 at 14:59
  • 1
    The only thing you need to change in above snippet is version of spring-mvc.xsd - http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd – Eugene Kuleshov Feb 10 '12 at 17:34
  • in that case try the below one xmlns:beans="http://www.springframework.org/schema/beans" I had the same problem and this solved it .. – Muthu Raman Jun 16 '14 at 14:11
  • You should (almost) never use versioned schema. See http://stackoverflow.com/questions/20894695/spring-configuration-xml-schema-with-or-without-version/20900801#20900801 – Brian Clozel Feb 05 '15 at 08:48
6

When using Spring namespaces urls I normally do not use version information and that works most of the time pretty well. You might like to try the namespace url

http://www.springframework.org/schema/mvc/spring-mvc.xsd

instead of

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
lauwie
  • 301
  • 1
  • 5
  • I don't think it is desirable to use version in xsd declaration. Spring will use highest version in the project's dependencies if version not given. – Aniket Thakur Dec 26 '15 at 16:12
2

I was getting the same error. The cause was the missing Maven dependency spring -webmvc. I included the below dependency and it started working.

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
Vivek Kumar
  • 219
  • 2
  • 5
1

I think your schemaLocation mapping is incorrect. The namespace is specified as:

xmlns:mvc="http://www.springframework.org/schema/mvc"

which is correct, I believe, but in the schemaLocation you have

http://www.springframework.org/schema/mvc/spring-mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

So if you change the first line of the schemaLocation mapping to your mvc namespace, it should work fine.

dlaidlaw
  • 1,643
  • 17
  • 16
0

I have enroled for spring course on udemy. I followed every step that my instructor show me to do. So if you are using spring mvc and hibernate you may encounter this error Failed to read schema document 'http://www.springframework.org/schema/tx/spring-tx.xsd' etc for:

<mvc:annotation-driven/> and <tx:annotation-driven transaction-manager="myTransactionManager" /> elements

in my spring configuration file i had these two urls

    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd

    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd

in xsi:schemaLocation, which i replaced with

    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

actually i visited these two sites http://www.springframework.org/schema/mvc/ and http://www.springframework.org/schema/tx/ and just added the latest version of spring-mvc and spring-tx i.e, spring-mvc-4.2.xsd and spring-tx-4.2.xsd

So, in my opinion specifying version no explicitly is a good practice. It worked for me, hope this works for you too. Thank you.

Shafqat Shafi
  • 1,108
  • 17
  • 30