Spring v3 找不到元素“mvc:resources”的声明;

发布于 2024-12-04 16:51:13 字数 2206 浏览 1 评论 0原文

当前运行的

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 部分代码

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

Servlet 用途

web.xml 将除 mvc:resources 映射静态文件之外的所有 url 映射到 servlet 。

Bug

  • cvc-complex-type.2.4.c:匹配通配符严格,但找不到元素“mvc:annotation-driven”的声明。 app-servlet.xml /app/www/WEB-INF

  • cvc-complex-type.2.4.c:匹配通配符严格,但找不到元素“mvc:resources”的声明。 app-servlet.xml /app/www/WEB-INF

已知问题

问题

我该如何修复编译错误以使 mvc:resources 正常工作?

我已经为此挖掘了大约两个小时,但还没有可靠的答案......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撩动你心 2024-12-11 16:51:13

在 spring 上下文中,xml mvc 命名空间 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">

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

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.

微凉徒眸意 2024-12-11 16:51:13

当使用 Spring 命名空间 url 时,我通常不使用版本信息,并且
大部分时间都工作得很好。
您可能想尝试命名空间 url

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

而不是

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

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
杯别 2024-12-11 16:51:13

我遇到了同样的错误。原因是缺少 Maven 依赖项 spring -webmvc。我添加了以下依赖项,它开始工作。

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

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>
绝影如岚 2024-12-11 16:51:13

我认为您的 schemaLocation 映射不正确。命名空间被指定为:

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

我相信这是正确的,但是在 schemaLocation 中你有

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

所以如果你将 schemaLocation 映射的第一行更改为你的 mvc 命名空间,它应该可以正常工作。

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.

一个人的夜不怕黑 2024-12-11 16:51:13

我已报名参加 udemy 春季课程。我遵循教练指示我做的每一步。
所以如果你使用 spring mvc 和 hibernate 你可能会遇到这个错误
无法读取架构文档 'http://www.springframework.org/schema/ tx/spring-tx.xsd' 等:

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

在我的 spring 配置文件中,我

    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

在 xsi:schemaLocation 中有这两个 url,我将其替换为

    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

实际上我访问了这两个站点
http://www.springframework.org/schema/mvc/http://www.springframework.org/schema/tx/
并且刚刚添加了最新版本的 spring-mvc 和 spring-tx 即 spring-mvc-4.2.xsd 和 spring-tx-4.2.xsd

因此,在我看来,明确指定版本是一个很好的做法。
它对我有用,希望这对你也有用。
谢谢。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文