开发者

Spring MVC mvc:resources location attribute

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


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.


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/") ?


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜