Spring v3没有为元素'mvc:resources'找到声明

Luc*_*ure 26 java xml schema spring servlets

目前正在运行

Tomcat:v6

Spring Tools Suite:v2.7.2

Spring框架: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>
Run Code Online (Sandbox Code Playgroud)

web.xml部分代码

<servlet-mapping>
    <servlet-name>duckapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

Servlet目的

web.xml将所有URL映射到servlet,但mvc:resources映射静态文件除外.

错误

  • cvc-complex-type.2.4.c:匹配的通配符是strict,但是没有找到元素'mvc:annotation-driven'的声明.app-servlet.xml/app/www/WEB-INF

  • cvc-complex-type.2.4.c:匹配的通配符是strict,但是没有为元素'mvc:resources'找到声明.app-servlet.xml/app/www/WEB-INF

已知的问题

如何修复编译错误以使mvc:资源正常工作?

我一直在挖掘大约2个小时,还没有可靠的答案......

Eug*_*hov 46

在您的spring上下文中,xml mvc namespace url应该与schemaLocation中的url匹配.像这样的东西:

<?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">
Run Code Online (Sandbox Code Playgroud)

这是标准的XML命名空间声明.命名空间url是一个唯一的id,然后映射到xsi:schemaLocation中的实际架构位置.


lau*_*wie 6

当使用Spring命名空间URL时,我通常不使用版本信息,并且大部分时间都可以正常工作.您可能想尝试命名空间URL

http://www.springframework.org/schema/mvc/spring-mvc.xsd
Run Code Online (Sandbox Code Playgroud)

代替

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
Run Code Online (Sandbox Code Playgroud)