Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
20fcefc6
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
20fcefc6
编写于
11月 19, 2017
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ResourceHttpRequestHandler uses EmbeddedValueResolverAware
上级
14a7da81
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
43 deletion
+47
-43
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java
...ervlet/config/annotation/ResourceHandlerRegistration.java
+3
-8
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
...work/web/servlet/resource/ResourceHttpRequestHandler.java
+44
-35
未找到文件。
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistration.java
浏览文件 @
20fcefc6
...
...
@@ -99,9 +99,7 @@ public class ResourceHandlerRegistration {
/**
* Specify the {@link org.springframework.http.CacheControl} which should be used
* by the resource handler.
*
* <p>Setting a custom value here will override the configuration set with {@link #setCachePeriod}.
*
* @param cacheControl the CacheControl configuration to use
* @return the same {@link ResourceHandlerRegistration} instance, for chained method invocation
* @since 4.2
...
...
@@ -114,11 +112,9 @@ public class ResourceHandlerRegistration {
/**
* Configure a chain of resource resolvers and transformers to use. This
* can be useful, for example, to apply a version strategy to resource URLs.
*
* <p>If this method is not invoked, by default only a simple
* {@link PathResourceResolver} is used in order to match URL paths to
* resources under the configured locations.
*
* @param cacheResources whether to cache the result of resource resolution;
* setting this to "true" is recommended for production (and "false" for
* development, especially when applying a version strategy)
...
...
@@ -133,11 +129,9 @@ public class ResourceHandlerRegistration {
/**
* Configure a chain of resource resolvers and transformers to use. This
* can be useful, for example, to apply a version strategy to resource URLs.
*
* <p>If this method is not invoked, by default only a simple
* {@link PathResourceResolver} is used in order to match URL paths to
* resources under the configured locations.
*
* @param cacheResources whether to cache the result of resource resolution;
* setting this to "true" is recommended for production (and "false" for
* development, especially when applying a version strategy
...
...
@@ -154,15 +148,16 @@ public class ResourceHandlerRegistration {
return
this
.
resourceChainRegistration
;
}
/**
* Return
s
the URL path patterns for the resource handler.
* Return the URL path patterns for the resource handler.
*/
protected
String
[]
getPathPatterns
()
{
return
this
.
pathPatterns
;
}
/**
* Return
s
a {@link ResourceHttpRequestHandler} instance.
* Return a {@link ResourceHttpRequestHandler} instance.
*/
protected
ResourceHttpRequestHandler
getRequestHandler
()
{
ResourceHttpRequestHandler
handler
=
new
ResourceHttpRequestHandler
();
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
浏览文件 @
20fcefc6
...
...
@@ -31,8 +31,8 @@ import org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.config.ConfigurableBeanFactory
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.EmbeddedValueResolverAware
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.UrlResource
;
import
org.springframework.http.HttpHeaders
;
...
...
@@ -49,6 +49,7 @@ import org.springframework.util.CollectionUtils;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.util.ResourceUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringValueResolver
;
import
org.springframework.web.HttpRequestHandler
;
import
org.springframework.web.accept.ContentNegotiationManager
;
import
org.springframework.web.accept.PathExtensionContentNegotiationStrategy
;
...
...
@@ -93,19 +94,19 @@ import org.springframework.web.util.UrlPathHelper;
* @since 3.0.4
*/
public
class
ResourceHttpRequestHandler
extends
WebContentGenerator
implements
HttpRequestHandler
,
InitializingBean
,
CorsConfigurationSource
{
implements
HttpRequestHandler
,
EmbeddedValueResolverAware
,
InitializingBean
,
CorsConfigurationSource
{
private
static
final
Log
logger
=
LogFactory
.
getLog
(
ResourceHttpRequestHandler
.
class
);
private
static
final
String
URL_RESOURCE_CHARSET_PREFIX
=
"[charset="
;
private
final
List
<
String
>
locationValues
=
new
ArrayList
<>(
4
);
private
final
List
<
Resource
>
locations
=
new
ArrayList
<>(
4
);
private
final
Map
<
Resource
,
Charset
>
locationCharsets
=
new
HashMap
<>(
4
);
private
final
List
<
String
>
locationValues
=
new
ArrayList
<>(
4
);
private
final
List
<
ResourceResolver
>
resourceResolvers
=
new
ArrayList
<>(
4
);
private
final
List
<
ResourceTransformer
>
resourceTransformers
=
new
ArrayList
<>(
4
);
...
...
@@ -128,12 +129,29 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
@Nullable
private
UrlPathHelper
urlPathHelper
;
@Nullable
private
StringValueResolver
embeddedValueResolver
;
public
ResourceHttpRequestHandler
()
{
super
(
HttpMethod
.
GET
.
name
(),
HttpMethod
.
HEAD
.
name
());
}
/**
* An alternative to {@link #setLocations(List)} that accepts a list of
* String-based location values, with support for {@link UrlResource}'s
* (e.g. files or HTTP URLs) with a special prefix to indicate the charset
* to use when appending relative paths. For example
* {@code "[charset=Windows-31J]http://example.org/path"}.
* @since 4.3.13
*/
public
void
setLocationValues
(
List
<
String
>
locationValues
)
{
Assert
.
notNull
(
locationValues
,
"Location values list must not be null"
);
this
.
locationValues
.
clear
();
this
.
locationValues
.
addAll
(
locationValues
);
}
/**
* Set the {@code List} of {@code Resource} locations to use as sources
* for serving static resources.
...
...
@@ -147,28 +165,16 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
/**
* Return the configured {@code List} of {@code Resource} locations.
* Note that if {@link #setLocationValues(List) locationValues} are provided,
*
<p>
Note that if {@link #setLocationValues(List) locationValues} are provided,
* instead of loaded Resource-based locations, this method will return
* empty until after initialization via {@link #afterPropertiesSet()}.
* @see #setLocationValues
* @see #setLocations
*/
public
List
<
Resource
>
getLocations
()
{
return
this
.
locations
;
}
/**
* An alternative to {@link #setLocations(List)} that accepts a list of
* String-based location values, with support for {@link UrlResource}'s
* (e.g. files or HTTP URLs) with a special prefix to indicate the charset
* to use when appending relative paths. For example
* {@code "[charset=Windows-31J]http://example.org/path"}.
* @since 4.3.13
*/
public
void
setLocationValues
(
List
<
String
>
locationValues
)
{
Assert
.
notNull
(
locationValues
,
"Location values list must not be null"
);
this
.
locationValues
.
clear
();
this
.
locationValues
.
addAll
(
locationValues
);
}
/**
* Configure the list of {@link ResourceResolver}s to use.
* <p>By default {@link PathResourceResolver} is configured. If using this property,
...
...
@@ -246,7 +252,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
* Configure a {@code ContentNegotiationManager} to help determine the
* media types for resources being served. If the manager contains a path
* extension strategy it will be checked for registered file extension.
* @param contentNegotiationManager the manager in use
* @since 4.3
*/
public
void
setContentNegotiationManager
(
@Nullable
ContentNegotiationManager
contentNegotiationManager
)
{
...
...
@@ -298,11 +303,15 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
return
this
.
urlPathHelper
;
}
@Override
public
void
setEmbeddedValueResolver
(
StringValueResolver
resolver
)
{
this
.
embeddedValueResolver
=
resolver
;
}
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
loadResourceLocations
();
resolveResourceLocations
();
if
(
logger
.
isWarnEnabled
()
&&
CollectionUtils
.
isEmpty
(
this
.
locations
))
{
logger
.
warn
(
"Locations list is empty. No resources will be served unless a "
+
...
...
@@ -325,23 +334,23 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
this
.
contentNegotiationStrategy
=
initContentNegotiationStrategy
();
}
private
void
loadResourceLocations
()
{
if
(!
CollectionUtils
.
isEmpty
(
this
.
locations
)
&&
!
CollectionUtils
.
isEmpty
(
this
.
locationValues
))
{
throw
new
IllegalArgumentException
(
"Please set either Resource-based \"locations\" or "
+
"String-based \"locationValues\", but not both."
);
}
private
void
resolveResourceLocations
()
{
if
(
CollectionUtils
.
isEmpty
(
this
.
locationValues
))
{
return
;
}
ApplicationContext
appContext
=
obtainApplicationContext
();
ConfigurableBeanFactory
beanFactory
=
null
;
if
(
appContext
.
getAutowireCapableBeanFactory
()
instanceof
ConfigurableBeanFactory
)
{
beanFactory
=
((
ConfigurableBeanFactory
)
appContext
.
getAutowireCapableBeanFactory
());
else
if
(!
CollectionUtils
.
isEmpty
(
this
.
locations
))
{
throw
new
IllegalArgumentException
(
"Please set either Resource-based \"locations\" or "
+
"String-based \"locationValues\", but not both."
);
}
ApplicationContext
applicationContext
=
obtainApplicationContext
();
for
(
String
location
:
this
.
locationValues
)
{
if
(
beanFactory
!=
null
)
{
location
=
beanFactory
.
resolveEmbeddedValue
(
location
);
Assert
.
notNull
(
location
,
"Null location"
);
if
(
this
.
embeddedValueResolver
!=
null
)
{
String
resolvedLocation
=
this
.
embeddedValueResolver
.
resolveStringValue
(
location
);
if
(
resolvedLocation
==
null
)
{
throw
new
IllegalArgumentException
(
"Location resolved to null: "
+
location
);
}
location
=
resolvedLocation
;
}
Charset
charset
=
null
;
location
=
location
.
trim
();
...
...
@@ -354,7 +363,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
charset
=
Charset
.
forName
(
value
);
location
=
location
.
substring
(
endIndex
+
1
);
}
Resource
resource
=
appContext
.
getResource
(
location
);
Resource
resource
=
app
lication
Context
.
getResource
(
location
);
this
.
locations
.
add
(
resource
);
if
(
charset
!=
null
)
{
if
(!(
resource
instanceof
UrlResource
))
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录