文档章节

Spring MVC如何访问到静态的文件,如jpg,js,css?

Oscarfff
 Oscarfff
发布于 2015/06/09 17:48
字数 1579
阅读 3495
收藏 12

如果你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题。如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg的访问也就被拦截了。

目的:可以正常访问静态文件,不要找不到静态文件报404。

方案一:激活Tomcat的defaultServlet来处理静态文件

Xml代码

<servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>*.jpg</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>*.js</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>*.css</url-pattern>
 </servlet-mapping>

要写在DispatcherServlet的前面, 让 defaultServlet先拦截,这个就不会进入Spring了。

方案二: 在spring3.0.4以后版本提供了mvc:resources 的使用方法:

Xml代码

 <!-- 对静态资源文件的访问 -->
 <mvc:resources mapping="/images/**" location="/images/" />

/images/**映射到ResourceHttpRequestHandler进行处理,location指定静态资源的位置.可以是web application根目录下、jar包里面,这样可以把静态资源压缩到jar包中。cache-period 可以使得静态资源进行web cache 如果出现下面的错误,可能是没有配置<mvc:annotation-driven />的原因。 报错WARNING: No mapping found for HTTP request with URI [/mvc/user/findUser/lisi/770] in DispatcherServlet with name 'springMVC'

使用<mvc:resources/>元素,把mapping的URI注册到SimpleUrlHandlerMapping的urlMap中,key为mapping的URI pattern值,而value为ResourceHttpRequestHandler,这样就巧妙的把对静态资源的访问由HandlerMapping转到ResourceHttpRequestHandler处理并返回,所以就支持classpath目录,jar包内静态资源的访问.另外需要注意的一点是,不要对SimpleUrlHandlerMapping设置defaultHandler。因为对static uri的defaultHandler就是ResourceHttpRequestHandler,否则无法处理static resources request。

方案三 ,使用<mvc:default-servlet-handler/>

Xml代码

 <mvc:default-servlet-handler/>

会把"/**" url,注册到SimpleUrlHandlerMapping的urlMap中,把对静态资源的访问由HandlerMapping转到org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler处理并返回DefaultServletHttpRequestHandler使用,就是各个Servlet容器自己的默认Servlet。

补充说明:多个HandlerMapping的执行顺序问题:

DefaultAnnotationHandlerMapping的order属性值是:0

<mvc:resources/ >自动注册的SimpleUrlHandlerMapping的order属性值是:2147483646
<mvc:default-servlet-handler/>自动注册的SimpleUrlHandlerMapping的order属性值是:2147483647

spring会先执行order值比较小的。当访问一个a.jpg图片文件时,先通过DefaultAnnotationHandlerMapping来找处理器,一定是找不到的,我们没有叫a.jpg的Action。再按order值升序找,由于最后一个SimpleUrlHandlerMapping是匹配"/**"的,所以一定会匹配上,再响应图片。

注:如果DispatcherServlet拦截 *.do这样的URL,就不存上述问题了。

方案四、认真阅读spring的源码,好好理解一下。

17.16.7 Serving of Resources

This option allows static resource requests following a particular URL pattern to be served by a ResourceHttpRequestHandler from any of a list of Resource locations. This provides a convenient way to serve static resources from locations other than the web application root, including locations on the classpath. The cache-period property may be used to set far future expiration headers (1 year is the recommendation of optimization tools such as Page Speed and YSlow) so that they will be more efficiently utilized by the client. The handler also properly evaluates the Last-Modified header (if present) so that a 304 status code will be returned as appropriate, avoiding unnecessary overhead for resources that are already cached by the client. For example, to serve resource requests with a URL pattern of /resources/** from a public-resources directory within the web application root you would use:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/");
    }
}

And the same in XML:

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

To serve these resources with a 1-year future expiration to ensure maximum use of the browser cache and a reduction in HTTP requests made by the browser:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/").setCachePeriod(31556926);
    }
}

And in XML:

<mvc:resources mapping="/resources/**" location="/public-resources/" cache-period="31556926"/>

The mapping attribute must be an Ant pattern that can be used by SimpleUrlHandlerMapping, and the location attribute must specify one or more valid resource directory locations. Multiple resource locations may be specified using a comma-separated list of values. The locations specified will be checked in the specified order for the presence of the resource for any given request. For example, to enable the serving of resources from both the web application root and from a known path of /META-INF/public-web-resources/ in any jar on the classpath use:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/", "classpath:/META-INF/public-web-resources/");
    }
}

And in XML:

<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-web-resources/"/>

When serving resources that may change when a new version of the application is deployed it is recommended that you incorporate a version string into the mapping pattern used to request the resources so that you may force clients to request the newly deployed version of your application’s resources. Support for versioned URLs is built into the framework and can be enabled by configuring a resource chain on the resource handler. The chain consists of one more ResourceResolver instances followed by one or more ResourceTransformer instances. Together they can provide arbitrary resolution and transformation of resources.

The built-in VersionResourceResolver can be configured with different strategies. For example a FixedVersionStrategy can use a property, a date, or other as the version. A ContentVersionStrategy uses an MD5 hash computed from the content of the resource (known as "fingerprinting" URLs).

ContentVersionStrategy is a good default choice to use except in cases where it cannot be used (e.g. with JavaScript module loaders). You can configure different version strategies against different patterns as shown below. Keep in mind also that computing content-based versions is expensive and therefore resource chain caching should be enabled in production.

Java config example;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/public-resources/")
                .resourceChain(true).addResolver(
                    new VersionResourceResolver().addContentVersionStrategy("/**"));
    }
}

XML example:

<mvc:resources mapping="/resources/**" location="/public-resources/">
<mvc:resource-chain>
<mvc:resource-cache />
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
</mvc:version-resolver>
</mvc:resolvers>
</mvc:resource-chain>
</mvc:resources>

In order for the above to work the application must also render URLs with versions. The easiest way to do that is to configure the ResourceUrlEncodingFilter which wraps the response and overrides its encodeURL method. This will work in JSPs, FreeMarker, Velocity, and any other view technology that calls the response encodeURL method. Alternatively, an application can also inject and use directly the ResourceUrlProvider bean, which is automatically declared with the MVC Java config and the MVC namespace.

17.16.8 Falling Back On the "Default" Servlet To Serve Resources

This allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container’s default Servlet), while still allowing static resource requests to be handled by the container’s default Servlet. It configures a DefaultServletHttpRequestHandler with a URL mapping of "/**" and the lowest priority relative to other URL mappings.

This handler will forward all requests to the default Servlet. Therefore it is important that it remains last in the order of all other URL HandlerMappings. That will be the case if you use <mvc:annotation-driven> or alternatively if you are setting up your own customized HandlerMapping instance be sure to set its order property to a value lower than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.

To enable the feature using the default setup use:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

Or in XML:

<mvc:default-servlet-handler/>

The caveat to overriding the "/" Servlet mapping is that the RequestDispatcher for the default Servlet must be retrieved by name rather than by path. The DefaultServletHttpRequestHandler will attempt to auto-detect the default Servlet for the container at startup time, using a list of known names for most of the major Servlet containers (including Tomcat, Jetty, GlassFish, JBoss, Resin, WebLogic, and WebSphere). If the default Servlet has been custom configured with a different name, or if a different Servlet container is being used where the default Servlet name is unknown, then the default Servlet’s name must be explicitly provided as in the following example:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable("myCustomDefaultServlet");
    }
}

Or in XML:

<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/>


© 著作权归作者所有

共有 人打赏支持
Oscarfff
粉丝 73
博文 815
码字总数 96913
作品 0
崇明
后端工程师
私信 提问
spring mvc - 对静态资源的处理

spring mvc - 对静态资源的处理 spring对静态资源文件(js css htm html jpg jepg png bmp)等的处理方式如下: 一、交由上级web容器处理,在web.xml中的org.springframework.web.servlet.Dis...

凯文加内特
2015/06/19
0
3
eclipse中启动spring-mvc项目时无法加载静态文件的问题分析

问题表现: eclipse里使用spring-mvc框架开发web项目,项目正常启动后能访问jsp页面,但jsp页面的网页效果没有生效,并提示jsp文件里提示js找不到,404错误。 (1)初步判断: 在浏览器里deb...

lilugoodjob
2018/07/02
0
0
Spring 静态资源被拦截

在spring-mvc.xml中配置了,让拦截器放弃对静态资源拦截,不起作用。 在applicationContext-security.xml中,进行静态资源映射: 同样不起作用:无法通过URL访问静态资源,究竟是什么原因...

Manfred.Yan
2013/01/06
1K
2
spring mvc 的url匹配怎么就这么烦呢?谁来解释解释

这几天一直在学习和整合spring mvc,在使用拦截器的和静态资源加载的时候url正则就烦了,谁来帮我讲讲这个如何操作。 以下是拦截器的配置: 说明下,配置成 /*/* 是为了能拦截到 /xx/xx 这样...

黄文祥
2013/03/08
1K
5
spring mvc + freemarker的js导入问题

最近应为要用spring mvc + freemarker ,按着网上的说法搭了下环境,发现导入静态文件的时候css,img可以导入,如果一导入js的话,html就解析不出来了,不知道什么情况,配置如下: spring m...

duoduo3_69
2013/10/05
4.1K
2

没有更多内容

加载失败,请刷新页面

加载更多

Hyperledger Fabric 实战(十): Fabric node SDK 样例 - 投票DAPP

Fabric node SDK 样例 - 投票DAPP 参考 fabric-samples 下的 fabcar 加以实现 目录结构 . ├── app │ ├── controllers │ │ └── index.js │ ├── enrollAdmin.js │ ├── in...

Jonluo
9分钟前
0
0
Redis高级特性及优化

redis中键的生存时间(expire) redis中可以使用expire命令设置一个键的生存时间,到时间后redis会自动删除它。 过期时间可以设置为秒或者毫秒精度。 过期时间分辨率总是 1 毫秒。 过期信息被...

rechardchensir
14分钟前
0
0
数据库设计-邮箱地址

1.前言 当我们涉及到要存储邮箱地址的时候,我们都知道应该存储为字符型,但是应该存储多长呢? 2.Stackoverflow StackOverflow一直是搜索答案的利器,这一次也不例外。这里多说一句,其实在...

hengbao5
18分钟前
0
0
uml类图

平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道。实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一、类的属性的表示方式 在UML类图中,...

asdf08442a
21分钟前
1
0
Linux下执行脚本报错提示invalid option 3: set: -

1、在环境下执行sh报错invalid option 3: set: - 2、于是换成chmod 777 install.sh改执行权限,并且默认使用#!/bin/bash bash: ./install.sh: /bin/bash^M: bad interpreter: No such file ......

noob_chr
22分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部