Spring MVC mvc:资源位置属性

发布于 2024-10-25 00:52:41 字数 752 浏览 1 评论 0原文

伙计们,我在加载静态资源时也遇到问题。

我想我已经把一切都设置好了。但我不明白 mvc:resourceslocation 属性。它是做什么用的?

如果我的静态资源位于 VAADIN/themes/theme/... 位置(在多个子文件夹、图像、css、js 中),位置和映射属性的正确值应该是什么?

当我在配置中有以下内容时:

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

它不起作用。我想日志的以下部分是相关的:

17:15:02.897 [http-8080-2] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#6': no
 URL paths identified

我收到 HTTP Status 404 例如 http://127.0.0.1/VAADIN/themes/theme/css/style.css

Guys I have also problem with loading static resources.

I think I have everything properly set up. But I don't understand location attribute of mvc:resources. What is it for?

if I have my static resources under location VAADIN/themes/theme/... (in several subfolders, images, css, js) what should be the correct values for location and mapping attributes?

When I have following in configuration:

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

it doesn't work. I suppose the following part of log is related:

17:15:02.897 [http-8080-2] DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Rejected bean name 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#6': no
 URL paths identified

I receive HTTP Status 404 for e.g. http://127.0.0.1/VAADIN/themes/theme/css/style.css

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

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

发布评论

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

评论(4

压抑⊿情绪 2024-11-01 00:52:41

location 是放置资源的文件夹的位置。 XSD 文档写道:

提供静态内容的资源位置,在 Spring 资源模式中指定。
每个位置必须指向一个有效的目录。多个位置可以指定为逗号分隔的列表,
并且将按照指定的顺序检查给定资源的位置。例如,值为
“/, classpath:/META-INF/public-web-resources/” 将允许从 Web 应用程序提供资源
root 以及包含 /META-INF/public-web-resources/ 目录的类路径上的任何 JAR,
Web 应用程序根目录中的资源优先。

另一方面,mapping 属性是:

当前 Servlet 上下文中的 URL 映射模式,用于从此处理程序提供资源,例如“/resources/**”

因此 mapping 指定在哪个 uri 下可以在 Web 上访问资源,而 location 指定这些资源位于磁盘上的位置。

location is the location to the folder where the resources are placed. The XSD docs write:

The resource location from which to serve static content, specified at a Spring Resource pattern.
Each location must point to a valid directory. Multiple locations may be specified as a comma-separated list,
and the locations will be checked for a given resource in the order specified. For example, a value of
"/, classpath:/META-INF/public-web-resources/" will allow resources to be served both from the web app
root and from any JAR on the classpath that contains a /META-INF/public-web-resources/ directory,
with resources in the web app root taking precedence.

On the other hand, the mapping attribute is:

The URL mapping pattern, within the current Servlet context, to use for serving resources from this handler, such as "/resources/**"

So mapping specifies under what uri will resources be accessible on the web, while location specifies where are these resources located on the disk.

晨敛清荷 2024-11-01 00:52:41

我的猜测是您没有正确引用该位置。

  • 您的 VAADIN 文件夹是否位于
    WAR的顶级目录(其中
    情况 location="/VAADIN/"
    正确)
  • 还是在 WEB-INF/classes 中(其中
    情况一定是
    位置=“类路径:/VAADIN/”)?

My Guess is that you're not referencing the location correctly.

  • Is your VAADIN folder inside the
    WAR's top level directory (in which
    case location="/VAADIN/" is
    correct)
  • or is it in WEB-INF/classes (in which
    case it must be
    location="classpath:/VAADIN/") ?
孤单情人 2024-11-01 00:52:41

也许您已经配置了一些在resourcehttprequestresolver(或任何它的名称)之前启动的处理程序映射
检查您是否没有 AbstractUrlHandlerMapping 或任何其他处理程序映射来停止具有 order 属性的映射链。
或者使用以下顺序配置资源解析器:

<!-- Maps all other request URLs to views -->
<bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="defaultHandler">
        <!-- Selects view names to render based on the request URI: e.g. the "/Home" URL would map to the view named "Home" -->
        <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    </property>
    <!-- This will prevent the mvc:resources to handle requests. Unless, of course, you specify an order in the mvc:resources order attribute
    <property name="order" value="3" /  Removing this will place this just after The ResourceHttpRequestHandler-->
</bean>

Maybe you have configured some handlermapping that kicks in before the resourcehttprequestresolver (or whatever it's called)
Check that you don't have an AbstractUrlHandlerMapping or any other handlermapping that stops the mapping chain with an order property.
Or configure the resource resolver with an order: <mvc:resources ... order="1" />

<!-- Maps all other request URLs to views -->
<bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="defaultHandler">
        <!-- Selects view names to render based on the request URI: e.g. the "/Home" URL would map to the view named "Home" -->
        <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    </property>
    <!-- This will prevent the mvc:resources to handle requests. Unless, of course, you specify an order in the mvc:resources order attribute
    <property name="order" value="3" /  Removing this will place this just after The ResourceHttpRequestHandler-->
</bean>
遗心遗梦遗幸福 2024-11-01 00:52:41

设置一个处理程序来提供静态内容。映射属性
设置为 /resources/**,其中包含一个 Ant 风格的通配符来指示该路径
必须以 /resources 开头,但可以包含其任何子路径。位置属性
指示要提供的文件的位置。按照此处的配置,任何请求
以 /resources 开头的路径将自动从 /resources 提供服务
应用程序根目录下的文件夹。因此,我们所有的图像、样式表、
JavaScript 和其他静态内容需要保存在应用程序的 /resources 中
文件夹。

sets up a handler for serving static content. The mapping attribute
is set to /resources/**, which includes an Ant-style wildcard to indicate that the path
must begin with /resources, but may include any subpath thereof. The location attribute
indicates the location of the files to be served. As configured here, any requests
whose paths begin with /resources will be automatically served from the /resources
folder at the root of the application. Therefore, all of our images, stylesheets,
JavaScript, and other static content needs to be kept in the application’s /resources
folder.

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