Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
b2d88ba8
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,发现更多精彩内容 >>
提交
b2d88ba8
编写于
9月 15, 2011
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6464 Polish following code review.
上级
aeba9d24
变更
36
隐藏空白更改
内联
并排
Showing
36 changed file
with
429 addition
and
275 deletion
+429
-275
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FlashMap.java
...c/main/java/org/springframework/web/servlet/FlashMap.java
+25
-25
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FlashMapManager.java
...java/org/springframework/web/servlet/FlashMapManager.java
+1
-2
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java
.../servlet/config/AnnotationDrivenBeanDefinitionParser.java
+1
-0
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java
...servlet/config/annotation/WebMvcConfigurationSupport.java
+124
-74
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
.../method/annotation/ExceptionHandlerExceptionResolver.java
+1
-1
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
...t/mvc/method/annotation/RequestMappingHandlerAdapter.java
+15
-19
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java
.../mvc/method/annotation/ServletInvocableHandlerMethod.java
+4
-4
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/HttpEntityMethodProcessor.java
.../method/annotation/support/HttpEntityMethodProcessor.java
+1
-1
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ModelAndViewMethodReturnValueHandler.java
...otation/support/ModelAndViewMethodReturnValueHandler.java
+2
-2
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RedirectAttributesMethodArgumentResolver.java
...ion/support/RedirectAttributesMethodArgumentResolver.java
+4
-10
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestResponseBodyMethodProcessor.java
...nnotation/support/RequestResponseBodyMethodProcessor.java
+1
-1
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletResponseMethodArgumentResolver.java
...tation/support/ServletResponseMethodArgumentResolver.java
+2
-2
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ViewMethodReturnValueHandler.java
...thod/annotation/support/ViewMethodReturnValueHandler.java
+3
-3
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/DefaultFlashMapManager.java
...framework/web/servlet/support/DefaultFlashMapManager.java
+6
-4
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/RedirectView.java
...va/org/springframework/web/servlet/view/RedirectView.java
+5
-5
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/FlashMapTests.java
...t/java/org/springframework/web/servlet/FlashMapTests.java
+52
-4
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java
...let/config/AnnotationDrivenBeanDefinitionParserTests.java
+1
-0
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java
...et/config/annotation/WebMvcConfigurationSupportTests.java
+2
-0
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java
.../method/annotation/RequestMappingHandlerAdapterTests.java
+2
-2
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java
...method/annotation/ServletInvocableHandlerMethodTests.java
+6
-6
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandlerTests.java
...otation/support/DefaultMethodReturnValueHandlerTests.java
+2
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/HttpEntityMethodProcessorTests.java
...od/annotation/support/HttpEntityMethodProcessorTests.java
+5
-5
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ModelAndViewMethodReturnValueHandlerTests.java
...on/support/ModelAndViewMethodReturnValueHandlerTests.java
+1
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java
...ation/support/RequestPartMethodArgumentResolverTests.java
+1
-1
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestResponseBodyMethodProcessorTests.java
...tion/support/RequestResponseBodyMethodProcessorTests.java
+3
-3
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolverTests.java
...on/support/ServletRequestMethodArgumentResolverTests.java
+2
-2
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletResponseMethodArgumentResolverTests.java
...n/support/ServletResponseMethodArgumentResolverTests.java
+3
-3
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/support/DefaultFlashMapManagerTests.java
...work/web/servlet/support/DefaultFlashMapManagerTests.java
+2
-4
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/RedirectViewTests.java
...g/springframework/web/servlet/view/RedirectViewTests.java
+1
-1
org.springframework.web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java
...g/springframework/web/method/annotation/ModelFactory.java
+1
-1
org.springframework.web/src/main/java/org/springframework/web/method/support/HandlerMethodArgumentResolver.java
...ork/web/method/support/HandlerMethodArgumentResolver.java
+16
-9
org.springframework.web/src/main/java/org/springframework/web/method/support/HandlerMethodReturnValueHandler.java
...k/web/method/support/HandlerMethodReturnValueHandler.java
+15
-11
org.springframework.web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java
...ngframework/web/method/support/ModelAndViewContainer.java
+44
-32
org.springframework.web/src/main/java/org/springframework/web/util/WebUtils.java
.../src/main/java/org/springframework/web/util/WebUtils.java
+0
-25
org.springframework.web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java
...mework/web/method/support/ModelAndViewContainerTests.java
+75
-0
org.springframework.web/src/test/java/org/springframework/web/util/WebUtilsTests.java
...test/java/org/springframework/web/util/WebUtilsTests.java
+0
-11
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FlashMap.java
浏览文件 @
b2d88ba8
...
...
@@ -17,10 +17,10 @@
package
org.springframework.web.servlet
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.StringUtils
;
/**
* A FlashMap provides a way for one request to store attributes intended for
...
...
@@ -35,7 +35,7 @@ import org.springframework.beans.BeanUtils;
* recipient. On a redirect, the target URL is known and for example
* {@code org.springframework.web.servlet.view.RedirectView} has the
* opportunity to automatically update the current FlashMap with target
* URL information
.
* URL information.
*
* <p>Annotated controllers will usually not use this type directly.
* See {@code org.springframework.web.servlet.mvc.support.RedirectAttributes}
...
...
@@ -46,13 +46,13 @@ import org.springframework.beans.BeanUtils;
*
* @see FlashMapManager
*/
public
class
FlashMap
extends
HashMap
<
String
,
Object
>
implements
Comparable
<
FlashMap
>
{
public
final
class
FlashMap
extends
HashMap
<
String
,
Object
>
implements
Comparable
<
FlashMap
>
{
private
static
final
long
serialVersionUID
=
1L
;
private
String
targetRequestPath
;
private
final
M
ap
<
String
,
String
>
targetRequestParams
=
new
LinkedHash
Map
<
String
,
String
>();
private
final
M
ultiValueMap
<
String
,
String
>
targetRequestParams
=
new
LinkedMultiValue
Map
<
String
,
String
>();
private
long
expirationStartTime
;
...
...
@@ -93,17 +93,15 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
}
/**
* Provide request parameter
pairs to identify the request for this FlashMap.
*
Only simple type, non-null parameter values are us
ed.
* Provide request parameter
s identifying the request for this FlashMap.
*
Null or empty keys and values are skipp
ed.
* @param params a Map with the names and values of expected parameters.
* @see BeanUtils#isSimpleValueType(Class)
*/
public
FlashMap
addTargetRequestParams
(
M
ap
<
String
,
?
>
params
)
{
public
FlashMap
addTargetRequestParams
(
M
ultiValueMap
<
String
,
String
>
params
)
{
if
(
params
!=
null
)
{
for
(
String
name
:
params
.
keySet
())
{
Object
value
=
params
.
get
(
name
);
if
((
value
!=
null
)
&&
BeanUtils
.
isSimpleValueType
(
value
.
getClass
()))
{
this
.
targetRequestParams
.
put
(
name
,
value
.
toString
());
for
(
String
key
:
params
.
keySet
())
{
for
(
String
value
:
params
.
get
(
key
))
{
addTargetRequestParam
(
key
,
value
);
}
}
}
...
...
@@ -111,19 +109,21 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
}
/**
* Provide a request parameter
to identify
the request for this FlashMap.
* @param name the
name of the expected parameter, never
{@code null}
* @param value the
value for the expected parameter, never
{@code null}
* Provide a request parameter
identifying
the request for this FlashMap.
* @param name the
expected parameter name, skipped if
{@code null}
* @param value the
expected parameter value, skipped if
{@code null}
*/
public
FlashMap
addTargetRequestParam
(
String
name
,
String
value
)
{
this
.
targetRequestParams
.
put
(
name
,
value
.
toString
());
if
(
StringUtils
.
hasText
(
name
)
&&
StringUtils
.
hasText
(
value
))
{
this
.
targetRequestParams
.
add
(
name
,
value
);
}
return
this
;
}
/**
* Return the parameters identifying the target request, or an empty
M
ap.
* Return the parameters identifying the target request, or an empty
m
ap.
*/
public
Map
<
String
,
String
>
getTargetRequestParams
()
{
public
M
ultiValueM
ap
<
String
,
String
>
getTargetRequestParams
()
{
return
targetRequestParams
;
}
...
...
@@ -174,11 +174,11 @@ public class FlashMap extends HashMap<String, Object> implements Comparable<Flas
@Override
public
String
toString
()
{
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
"[Attributes="
).
append
(
super
.
toString
());
result
.
append
(
", targetRequestPath="
).
append
(
this
.
targetRequestPath
);
result
.
append
(
", targetRequestParams="
+
this
.
targetRequestParams
.
toString
()
).
append
(
"]"
);
return
result
.
toString
();
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"[Attributes="
).
append
(
super
.
toString
());
sb
.
append
(
", targetRequestPath="
).
append
(
this
.
targetRequestPath
);
sb
.
append
(
", targetRequestParams="
).
append
(
this
.
targetRequestParams
).
append
(
"]"
);
return
sb
.
toString
();
}
}
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/FlashMapManager.java
浏览文件 @
b2d88ba8
...
...
@@ -29,8 +29,7 @@ import javax.servlet.http.HttpServletRequest;
* instances are exposed as request attributes and are accessible via methods
* in {@code org.springframework.web.servlet.support.RequestContextUtils}.
*
* <p>Annotated controllers are most likely to store and access flash attributes
* through their model.
* <p>Annotated controllers will usually not use this FlashMap directly.
* See {@code org.springframework.web.servlet.mvc.support.RedirectAttributes}.
*
* @author Rossen Stoyanchev
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java
浏览文件 @
b2d88ba8
...
...
@@ -130,6 +130,7 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
methodAdapterDef
.
setRole
(
BeanDefinition
.
ROLE_INFRASTRUCTURE
);
methodAdapterDef
.
getPropertyValues
().
add
(
"webBindingInitializer"
,
bindingDef
);
methodAdapterDef
.
getPropertyValues
().
add
(
"messageConverters"
,
messageConverters
);
methodAdapterDef
.
getPropertyValues
().
add
(
"ignoreDefaultModelOnRedirect"
,
true
);
if
(
argumentResolvers
!=
null
)
{
methodAdapterDef
.
getPropertyValues
().
add
(
"customArgumentResolvers"
,
argumentResolvers
);
}
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java
浏览文件 @
b2d88ba8
...
...
@@ -73,42 +73,59 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
;
/**
* A base class that provides default configuration for Spring MVC applications by registering Spring MVC
* infrastructure components to be detected by the {@link DispatcherServlet}. Typically applications should not
* have to extend this class. A more likely place to start is to annotate an @{@link Configuration}
* class with @{@link EnableWebMvc} (see @{@link EnableWebMvc} and {@link WebMvcConfigurer} for details).
* A base class that provides default configuration for Spring MVC applications
* by registering Spring MVC infrastructure components to be detected by the
* {@link DispatcherServlet}. An application configuration class is not required
* to extend this class. A more likely place to start is to annotate
* an @{@link Configuration} class with @{@link EnableWebMvc}
* (see @{@link EnableWebMvc} and {@link WebMvcConfigurer} for details).
*
* <p>If using @{@link EnableWebMvc} does not give you all you need, consider extending directly from this
* class. Remember to add @{@link Configuration} to your subclass and @{@link Bean} to any superclass
* @{@link Bean} methods you choose to override.
* <p>If the customization options available with use of @{@link EnableWebMvc}
* are not enough, consider extending directly from this class and override the
* appropriate methods. Remember to add @{@link Configuration} to your subclass
* and @{@link Bean} to any superclass @{@link Bean} methods you override.
*
* <p>This class registers the following {@link HandlerMapping}s:</p>
* <ul>
* <li>{@link RequestMappingHandlerMapping} ordered at 0 for mapping requests to annotated controller methods.
* <li>{@link HandlerMapping} ordered at 1 to map URL paths directly to view names.
* <li>{@link BeanNameUrlHandlerMapping} ordered at 2 to map URL paths to controller bean names.
* <li>{@link HandlerMapping} ordered at {@code Integer.MAX_VALUE-1} to serve static resource requests.
* <li>{@link HandlerMapping} ordered at {@code Integer.MAX_VALUE} to forward requests to the default servlet.
* <li>{@link RequestMappingHandlerMapping}
* ordered at 0 for mapping requests to annotated controller methods.
* <li>{@link HandlerMapping}
* ordered at 1 to map URL paths directly to view names.
* <li>{@link BeanNameUrlHandlerMapping}
* ordered at 2 to map URL paths to controller bean names.
* <li>{@link HandlerMapping}
* ordered at {@code Integer.MAX_VALUE-1} to serve static resource requests.
* <li>{@link HandlerMapping}
* ordered at {@code Integer.MAX_VALUE} to forward requests to the default servlet.
* </ul>
*
* <p>Registers these {@link HandlerAdapter}s:
* <ul>
* <li>{@link RequestMappingHandlerAdapter} for processing requests with annotated controller methods.
* <li>{@link HttpRequestHandlerAdapter} for processing requests with {@link HttpRequestHandler}s.
* <li>{@link SimpleControllerHandlerAdapter} for processing requests with interface-based {@link Controller}s.
* <li>{@link RequestMappingHandlerAdapter}
* for processing requests with annotated controller methods.
* <li>{@link HttpRequestHandlerAdapter}
* for processing requests with {@link HttpRequestHandler}s.
* <li>{@link SimpleControllerHandlerAdapter}
* for processing requests with interface-based {@link Controller}s.
* </ul>
*
* <p>Registers a {@link HandlerExceptionResolverComposite} with this chain of exception resolvers:
* <p>Registers a {@link HandlerExceptionResolverComposite} with this chain of
* exception resolvers:
* <ul>
* <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through @{@link ExceptionHandler} methods.
* <li>{@link ResponseStatusExceptionResolver} for exceptions annotated with @{@link ResponseStatus}.
* <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring exception types
* <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions
* through @{@link ExceptionHandler} methods.
* <li>{@link ResponseStatusExceptionResolver} for exceptions annotated
* with @{@link ResponseStatus}.
* <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring
* exception types
* </ul>
*
* <p>Registers these other instances:
* <ul>
* <li>{@link FormattingConversionService} for use with annotated controller methods and the spring:eval JSP tag.
* <li>{@link Validator} for validating model attributes on annotated controller methods.
* <li>{@link FormattingConversionService}
* for use with annotated controller methods and the spring:eval JSP tag.
* <li>{@link Validator}
* for validating model attributes on annotated controller methods.
* </ul>
*
* @see EnableWebMvc
...
...
@@ -137,7 +154,8 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a {@link RequestMappingHandlerMapping} ordered at 0 for mapping requests to annotated controllers.
* Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
* requests to annotated controllers.
*/
@Bean
public
RequestMappingHandlerMapping
requestMappingHandlerMapping
()
{
...
...
@@ -148,8 +166,9 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Provides access to the shared handler interceptors used to configure {@link HandlerMapping} instances with.
* This method cannot be overridden, use {@link #addInterceptors(InterceptorRegistry)} instead.
* Provide access to the shared handler interceptors used to configure
* {@link HandlerMapping} instances with. This method cannot be overridden,
* use {@link #addInterceptors(InterceptorRegistry)} instead.
*/
protected
final
Object
[]
getInterceptors
()
{
if
(
interceptors
==
null
)
{
...
...
@@ -162,15 +181,17 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Override this method to add Spring MVC interceptors for pre/post-processing of controller invocation.
* Override this method to add Spring MVC interceptors for
* pre- and post-processing of controller invocation.
* @see InterceptorRegistry
*/
protected
void
addInterceptors
(
InterceptorRegistry
registry
)
{
}
/**
* Returns a handler mapping ordered at 1 to map URL paths directly to view names.
* To configure view controllers, override {@link #addViewControllers(ViewControllerRegistry)}.
* Return a handler mapping ordered at 1 to map URL paths directly to
* view names. To configure view controllers, override
* {@link #addViewControllers}.
*/
@Bean
public
HandlerMapping
viewControllerHandlerMapping
()
{
...
...
@@ -191,7 +212,8 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a {@link BeanNameUrlHandlerMapping} ordered at 2 to map URL paths to controller bean names.
* Return a {@link BeanNameUrlHandlerMapping} ordered at 2 to map URL
* paths to controller bean names.
*/
@Bean
public
BeanNameUrlHandlerMapping
beanNameHandlerMapping
()
{
...
...
@@ -202,8 +224,9 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a handler mapping ordered at Integer.MAX_VALUE-1 with mapped resource handlers.
* To configure resource handling, override {@link #addResourceHandlers(ResourceHandlerRegistry)}.
* Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped
* resource handlers. To configure resource handling, override
* {@link #addResourceHandlers}.
*/
@Bean
public
HandlerMapping
resourceHandlerMapping
()
{
...
...
@@ -222,9 +245,9 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Return
s a handler mapping ordered at Integer.MAX_VALUE with a mapped default servlet handler.
*
To configure "default" Servlet handling, override
*
{@link #configureDefaultServletHandling(DefaultServletHandlerConfigurer)
}.
* Return
a handler mapping ordered at Integer.MAX_VALUE with a mapped
*
default servlet handler. To configure "default" Servlet handling,
*
override {@link #configureDefaultServletHandling
}.
*/
@Bean
public
HandlerMapping
defaultServletHandlerMapping
()
{
...
...
@@ -243,12 +266,13 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a {@link RequestMappingHandlerAdapter} for processing requests through annotated controller methods.
* Consider overriding one of these other more fine-grained methods:
* Returns a {@link RequestMappingHandlerAdapter} for processing requests
* through annotated controller methods. Consider overriding one of these
* other more fine-grained methods:
* <ul>
* <li>{@link #addArgumentResolvers
(List)
} for adding custom argument resolvers.
* <li>{@link #addReturnValueHandlers
(List)
} for adding custom return value handlers.
* <li>{@link #configureMessageConverters
(List)
} for adding custom message converters.
* <li>{@link #addArgumentResolvers} for adding custom argument resolvers.
* <li>{@link #addReturnValueHandlers} for adding custom return value handlers.
* <li>{@link #configureMessageConverters} for adding custom message converters.
* </ul>
*/
@Bean
...
...
@@ -268,34 +292,46 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
adapter
.
setWebBindingInitializer
(
webBindingInitializer
);
adapter
.
setCustomArgumentResolvers
(
argumentResolvers
);
adapter
.
setCustomReturnValueHandlers
(
returnValueHandlers
);
adapter
.
setIgnoreDefaultModelOnRedirect
(
true
);
return
adapter
;
}
/**
* Add custom {@link HandlerMethodArgumentResolver}s to use in addition to the ones registered by default.
* <p>Custom argument resolvers are invoked before built-in resolvers except for those that rely on the presence
* of annotations (e.g. {@code @RequestParameter}, {@code @PathVariable}, etc.). The latter can be customized
* by configuring the {@link RequestMappingHandlerAdapter} directly.
* @param argumentResolvers the list of custom converters; initially an empty list.
* Add custom {@link HandlerMethodArgumentResolver}s to use in addition to
* the ones registered by default.
* <p>Custom argument resolvers are invoked before built-in resolvers
* except for those that rely on the presence of annotations (e.g.
* {@code @RequestParameter}, {@code @PathVariable}, etc.).
* The latter can be customized by configuring the
* {@link RequestMappingHandlerAdapter} directly.
* @param argumentResolvers the list of custom converters;
* initially an empty list.
*/
protected
void
addArgumentResolvers
(
List
<
HandlerMethodArgumentResolver
>
argumentResolvers
)
{
}
/**
* Add custom {@link HandlerMethodReturnValueHandler}s in addition to the ones registered by default.
* <p>Custom return value handlers are invoked before built-in ones except for those that rely on the presence
* of annotations (e.g. {@code @ResponseBody}, {@code @ModelAttribute}, etc.). The latter can be customized
* by configuring the {@link RequestMappingHandlerAdapter} directly.
* @param returnValueHandlers the list of custom handlers; initially an empty list.
* Add custom {@link HandlerMethodReturnValueHandler}s in addition to the
* ones registered by default.
* <p>Custom return value handlers are invoked before built-in ones except
* for those that rely on the presence of annotations (e.g.
* {@code @ResponseBody}, {@code @ModelAttribute}, etc.).
* The latter can be customized by configuring the
* {@link RequestMappingHandlerAdapter} directly.
* @param returnValueHandlers the list of custom handlers;
* initially an empty list.
*/
protected
void
addReturnValueHandlers
(
List
<
HandlerMethodReturnValueHandler
>
returnValueHandlers
)
{
}
/**
* Provides access to the shared {@link HttpMessageConverter}s used by the
* {@link RequestMappingHandlerAdapter} and the {@link ExceptionHandlerExceptionResolver}.
* This method cannot be overridden. Use {@link #configureMessageConverters(List)} instead.
* Also see {@link #addDefaultHttpMessageConverters(List)} that can be used to add default message converters.
* {@link RequestMappingHandlerAdapter} and the
* {@link ExceptionHandlerExceptionResolver}.
* This method cannot be overridden.
* Use {@link #configureMessageConverters(List)} instead.
* Also see {@link #addDefaultHttpMessageConverters(List)} that can be
* used to add default message converters.
*/
protected
final
List
<
HttpMessageConverter
<?>>
getMessageConverters
()
{
if
(
messageConverters
==
null
)
{
...
...
@@ -309,17 +345,20 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Override this method to add custom {@link HttpMessageConverter}s to use with
* the {@link RequestMappingHandlerAdapter} and the {@link ExceptionHandlerExceptionResolver}.
* Adding converters to the list turns off the default converters that would otherwise be registered by default.
* Also see {@link #addDefaultHttpMessageConverters(List)} that can be used to add default message converters.
* @param converters a list to add message converters to; initially an empty list.
* Override this method to add custom {@link HttpMessageConverter}s to use
* with the {@link RequestMappingHandlerAdapter} and the
* {@link ExceptionHandlerExceptionResolver}. Adding converters to the
* list turns off the default converters that would otherwise be registered
* by default. Also see {@link #addDefaultHttpMessageConverters(List)} that
* can be used to add default message converters.
* @param converters a list to add message converters to;
* initially an empty list.
*/
protected
void
configureMessageConverters
(
List
<
HttpMessageConverter
<?>>
converters
)
{
}
/**
*
A method available to subclasses
to add default {@link HttpMessageConverter}s.
*
Override this method
to add default {@link HttpMessageConverter}s.
* @param messageConverters the list to add the default message converters to
*/
protected
final
void
addDefaultHttpMessageConverters
(
List
<
HttpMessageConverter
<?>>
messageConverters
)
{
...
...
@@ -346,9 +385,9 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a {@link FormattingConversionService} for use with annotated
controller methods and the
*
{@code spring:eval} JSP tag. Also see {@link #addFormatters(FormatterRegistry)} as an alternative
* to overriding this method.
* Returns a {@link FormattingConversionService} for use with annotated
*
controller methods and the {@code spring:eval} JSP tag.
*
Also see {@link #addFormatters} as an alternative
to overriding this method.
*/
@Bean
public
FormattingConversionService
mvcConversionService
()
{
...
...
@@ -364,8 +403,9 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns {@link Validator} for validating {@code @ModelAttribute} and {@code @RequestBody} arguments of
* annotated controller methods. To configure a custom validation, override {@link #getValidator()}.
* Returns {@link Validator} for validating {@code @ModelAttribute}
* and {@code @RequestBody} arguments of annotated controller methods.
* To configure a custom validation, override {@link #getValidator()}.
*/
@Bean
Validator
mvcValidator
()
{
...
...
@@ -404,7 +444,8 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a {@link HttpRequestHandlerAdapter} for processing requests with {@link HttpRequestHandler}s.
* Returns a {@link HttpRequestHandlerAdapter} for processing requests
* with {@link HttpRequestHandler}s.
*/
@Bean
public
HttpRequestHandlerAdapter
httpRequestHandlerAdapter
()
{
...
...
@@ -412,7 +453,8 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a {@link SimpleControllerHandlerAdapter} for processing requests with interface-based controllers.
* Returns a {@link SimpleControllerHandlerAdapter} for processing requests
* with interface-based controllers.
*/
@Bean
public
SimpleControllerHandlerAdapter
simpleControllerHandlerAdapter
()
{
...
...
@@ -420,8 +462,9 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Returns a {@link HandlerExceptionResolverComposite} that contains a list of exception resolvers.
* To customize the list of exception resolvers, override {@link #configureHandlerExceptionResolvers(List)}.
* Returns a {@link HandlerExceptionResolverComposite} that contains a list
* of exception resolvers. To customize the list of exception resolvers,
* override {@link #configureHandlerExceptionResolvers(List)}.
*/
@Bean
HandlerExceptionResolver
handlerExceptionResolver
()
throws
Exception
{
...
...
@@ -439,21 +482,28 @@ public abstract class WebMvcConfigurationSupport implements ApplicationContextAw
}
/**
* Override this method to configure the list of {@link HandlerExceptionResolver}s to use.
* Adding resolvers to the list turns off the default resolvers that would otherwise be registered by default.
* Also see {@link #addDefaultHandlerExceptionResolvers(List)} that can be used to add the default exception resolvers.
* @param exceptionResolvers a list to add exception resolvers to; initially an empty list.
* Override this method to configure the list of
* {@link HandlerExceptionResolver}s to use. Adding resolvers to the list
* turns off the default resolvers that would otherwise be registered by
* default. Also see {@link #addDefaultHandlerExceptionResolvers(List)}
* that can be used to add the default exception resolvers.
* @param exceptionResolvers a list to add exception resolvers to;
* initially an empty list.
*/
protected
void
configureHandlerExceptionResolvers
(
List
<
HandlerExceptionResolver
>
exceptionResolvers
)
{
}
/**
* A method available to subclasses for adding default {@link HandlerExceptionResolver}s.
* A method available to subclasses for adding default
* {@link HandlerExceptionResolver}s.
* <p>Adds the following exception resolvers:
* <ul>
* <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through @{@link ExceptionHandler} methods.
* <li>{@link ResponseStatusExceptionResolver} for exceptions annotated with @{@link ResponseStatus}.
* <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring exception types
* <li>{@link ExceptionHandlerExceptionResolver}
* for handling exceptions through @{@link ExceptionHandler} methods.
* <li>{@link ResponseStatusExceptionResolver}
* for exceptions annotated with @{@link ResponseStatus}.
* <li>{@link DefaultHandlerExceptionResolver}
* for resolving known Spring exception types
* </ul>
*/
protected
final
void
addDefaultHandlerExceptionResolvers
(
List
<
HandlerExceptionResolver
>
exceptionResolvers
)
{
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
浏览文件 @
b2d88ba8
...
...
@@ -235,7 +235,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
return
null
;
}
if
(
!
mavContainer
.
isResolveView
())
{
if
(
mavContainer
.
isRequestHandled
())
{
return
new
ModelAndView
();
}
else
{
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
浏览文件 @
b2d88ba8
...
...
@@ -93,7 +93,6 @@ import org.springframework.web.servlet.mvc.method.annotation.support.ServletRequ
import
org.springframework.web.servlet.mvc.method.annotation.support.ServletResponseMethodArgumentResolver
;
import
org.springframework.web.servlet.mvc.method.annotation.support.ViewMethodReturnValueHandler
;
import
org.springframework.web.servlet.mvc.support.RedirectAttributes
;
import
org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap
;
import
org.springframework.web.servlet.support.RequestContextUtils
;
import
org.springframework.web.util.WebUtils
;
...
...
@@ -147,7 +146,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter i
private
SessionAttributeStore
sessionAttributeStore
=
new
DefaultSessionAttributeStore
();
private
boolean
alwaysUseRedirectAttributes
;
private
boolean
ignoreDefaultModelOnRedirect
=
false
;
private
final
Map
<
Class
<?>,
SessionAttributesHandler
>
sessionAttributesHandlerCache
=
new
ConcurrentHashMap
<
Class
<?>,
SessionAttributesHandler
>();
...
...
@@ -334,19 +333,20 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter i
}
/**
* By default a controller uses {@link Model} to select attributes for
* rendering and for redirecting. However, a controller can also use
* {@link RedirectAttributes} to select attributes before a redirect.
* <p>When this flag is set to {@code true}, {@link RedirectAttributes}
* becomes the only way to select attributes for a redirect.
* In other words, for a redirect a controller must use
* {@link RedirectAttributes} or no attributes will be used.
* <p>The default value is {@code false}, meaning the {@link Model} is
* used unless {@link RedirectAttributes} is used.
* A controller can use the "default" {@link Model} in rendering and
* redirect scenarios. Alternatively, it can use {@link RedirectAttributes}
* to provide attributes for a redirect scenario.
* <p>When this flag is set to {@code true}, the "default" model is never
* used in a redirect even if the controller method doesn't explicitly
* declare a RedirectAttributes argument.
* <p>When set to {@code false}, the "default" model may be used in a
* redirect if the controller method doesn't explicitly declare a
* RedirectAttributes argument.
* <p>The default setting is {@code false}.
* @see RedirectAttributes
*/
public
void
set
AlwaysUseRedirectAttributes
(
boolean
alwaysUseRedirectAttributes
)
{
this
.
alwaysUseRedirectAttributes
=
alwaysUseRedirectAttributes
;
public
void
set
IgnoreDefaultModelOnRedirect
(
boolean
ignoreDefaultModelOnRedirect
)
{
this
.
ignoreDefaultModelOnRedirect
=
ignoreDefaultModelOnRedirect
;
}
public
void
setBeanFactory
(
BeanFactory
beanFactory
)
{
...
...
@@ -538,18 +538,14 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter i
ModelAndViewContainer
mavContainer
=
new
ModelAndViewContainer
();
mavContainer
.
addAllAttributes
(
RequestContextUtils
.
getInputFlashMap
(
request
));
modelFactory
.
initModel
(
webRequest
,
mavContainer
,
requestMappingMethod
);
mavContainer
.
setIgnoreDefaultModelOnRedirect
(
this
.
ignoreDefaultModelOnRedirect
);
if
(
this
.
alwaysUseRedirectAttributes
)
{
DataBinder
dataBinder
=
binderFactory
.
createBinder
(
webRequest
,
null
,
null
);
mavContainer
.
setRedirectModel
(
new
RedirectAttributesModelMap
(
dataBinder
));
}
SessionStatus
sessionStatus
=
new
SimpleSessionStatus
();
requestMappingMethod
.
invokeAndHandle
(
webRequest
,
mavContainer
,
sessionStatus
);
modelFactory
.
updateModel
(
webRequest
,
mavContainer
,
sessionStatus
);
if
(
!
mavContainer
.
isResolveView
())
{
if
(
mavContainer
.
isRequestHandled
())
{
return
null
;
}
else
{
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java
浏览文件 @
b2d88ba8
...
...
@@ -75,7 +75,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
* <p>Return value handling may be skipped entirely when the method returns {@code null} (also possibly due
* to a {@code void} return type) and one of the following additional conditions is true:
* <ul>
* <li>A {@link HandlerMethodArgumentResolver} has set the {@link ModelAndViewContainer#setRe
solveView
(boolean)}
* <li>A {@link HandlerMethodArgumentResolver} has set the {@link ModelAndViewContainer#setRe
questHandled
(boolean)}
* flag to {@code false} -- e.g. method arguments providing access to the response.
* <li>The request qualifies as "not modified" as defined in {@link ServletWebRequest#checkNotModified(long)}
* and {@link ServletWebRequest#checkNotModified(String)}. In this case a response with "not modified" response
...
...
@@ -98,13 +98,13 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
setResponseStatus
((
ServletWebRequest
)
request
);
if
(
returnValue
==
null
)
{
if
(
isRequestNotModified
(
request
)
||
hasResponseStatus
()
||
!
mavContainer
.
isResolveView
())
{
mavContainer
.
setRe
solveView
(
fals
e
);
if
(
isRequestNotModified
(
request
)
||
hasResponseStatus
()
||
mavContainer
.
isRequestHandled
())
{
mavContainer
.
setRe
questHandled
(
tru
e
);
return
;
}
}
mavContainer
.
setRe
solveView
(
tru
e
);
mavContainer
.
setRe
questHandled
(
fals
e
);
try
{
returnValueHandlers
.
handleReturnValue
(
returnValue
,
getReturnType
(),
mavContainer
,
request
);
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/HttpEntityMethodProcessor.java
浏览文件 @
b2d88ba8
...
...
@@ -101,7 +101,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
ModelAndViewContainer
mavContainer
,
NativeWebRequest
webRequest
)
throws
Exception
{
mavContainer
.
setRe
solveView
(
fals
e
);
mavContainer
.
setRe
questHandled
(
tru
e
);
if
(
returnValue
==
null
)
{
return
;
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ModelAndViewMethodReturnValueHandler.java
浏览文件 @
b2d88ba8
...
...
@@ -24,7 +24,7 @@ import org.springframework.web.servlet.ModelAndView;
/**
* Handles return values of type {@link ModelAndView} transferring their content to the {@link ModelAndViewContainer}.
* If the return value is {@code null}, the {@link ModelAndViewContainer#setRe
solveView
(boolean)} flag is set to
* If the return value is {@code null}, the {@link ModelAndViewContainer#setRe
questHandled
(boolean)} flag is set to
* {@code false} to indicate view resolution is not needed.
*
* @author Rossen Stoyanchev
...
...
@@ -49,7 +49,7 @@ public class ModelAndViewMethodReturnValueHandler implements HandlerMethodReturn
mavContainer
.
addAllAttributes
(
mav
.
getModel
());
}
else
{
mavContainer
.
setRe
solveView
(
fals
e
);
mavContainer
.
setRe
questHandled
(
tru
e
);
}
}
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RedirectAttributesMethodArgumentResolver.java
浏览文件 @
b2d88ba8
...
...
@@ -49,16 +49,10 @@ public class RedirectAttributesMethodArgumentResolver implements HandlerMethodAr
ModelAndViewContainer
mavContainer
,
NativeWebRequest
webRequest
,
WebDataBinderFactory
binderFactory
)
throws
Exception
{
if
(
mavContainer
.
getRedirectModel
()
!=
null
)
{
return
mavContainer
.
getRedirectModel
();
}
else
{
DataBinder
dataBinder
=
binderFactory
.
createBinder
(
webRequest
,
null
,
null
);
ModelMap
redirectAttributes
=
new
RedirectAttributesModelMap
(
dataBinder
);
mavContainer
.
setRedirectModel
(
redirectAttributes
);
return
redirectAttributes
;
}
DataBinder
dataBinder
=
binderFactory
.
createBinder
(
webRequest
,
null
,
null
);
ModelMap
redirectAttributes
=
new
RedirectAttributesModelMap
(
dataBinder
);
mavContainer
.
setRedirectModel
(
redirectAttributes
);
return
redirectAttributes
;
}
}
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestResponseBodyMethodProcessor.java
浏览文件 @
b2d88ba8
...
...
@@ -98,7 +98,7 @@ public class RequestResponseBodyMethodProcessor extends AbstractMessageConverter
MethodParameter
returnType
,
ModelAndViewContainer
mavContainer
,
NativeWebRequest
webRequest
)
throws
IOException
,
HttpMediaTypeNotAcceptableException
{
mavContainer
.
setRe
solveView
(
fals
e
);
mavContainer
.
setRe
questHandled
(
tru
e
);
if
(
returnValue
!=
null
)
{
writeWithMessageConverters
(
returnValue
,
returnType
,
webRequest
);
}
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletResponseMethodArgumentResolver.java
浏览文件 @
b2d88ba8
...
...
@@ -54,7 +54,7 @@ public class ServletResponseMethodArgumentResolver implements HandlerMethodArgum
/**
* {@inheritDoc}
* <p>Sets the {@link ModelAndViewContainer#setRe
solveView
(boolean)} flag to {@code false} to indicate
* <p>Sets the {@link ModelAndViewContainer#setRe
questHandled
(boolean)} flag to {@code false} to indicate
* that the method signature provides access to the response. If subsequently the underlying method
* returns {@code null}, view resolution will be bypassed.
* @see ServletInvocableHandlerMethod#invokeAndHandle(NativeWebRequest, ModelAndViewContainer, Object...)
...
...
@@ -64,7 +64,7 @@ public class ServletResponseMethodArgumentResolver implements HandlerMethodArgum
NativeWebRequest
webRequest
,
WebDataBinderFactory
binderFactory
)
throws
IOException
{
mavContainer
.
setRe
solveView
(
fals
e
);
mavContainer
.
setRe
questHandled
(
tru
e
);
HttpServletResponse
response
=
webRequest
.
getNativeResponse
(
HttpServletResponse
.
class
);
Class
<?>
paramType
=
parameter
.
getParameterType
();
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/method/annotation/support/ViewMethodReturnValueHandler.java
浏览文件 @
b2d88ba8
...
...
@@ -61,14 +61,14 @@ public class ViewMethodReturnValueHandler implements HandlerMethodReturnValueHan
String
viewName
=
(
String
)
returnValue
;
mavContainer
.
setViewName
(
viewName
);
if
(
isRedirectViewName
(
viewName
))
{
mavContainer
.
setUseRedirectModel
();
mavContainer
.
setUseRedirectModel
(
true
);
}
}
else
if
(
returnValue
instanceof
View
){
View
view
=
(
View
)
returnValue
;
mavContainer
.
setView
(
view
);
if
(
isRedirectView
(
view
))
{
mavContainer
.
setUseRedirectModel
();
mavContainer
.
setUseRedirectModel
(
true
);
}
}
else
{
...
...
@@ -95,7 +95,7 @@ public class ViewMethodReturnValueHandler implements HandlerMethodReturnValueHan
* "false" otherwise.
*/
protected
boolean
isRedirectView
(
View
view
)
{
if
(
SmartView
.
class
.
isAssignableFrom
(
view
.
getClass
())
)
{
if
(
view
instanceof
SmartView
)
{
return
((
SmartView
)
view
).
isRedirectView
();
}
else
{
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/DefaultFlashMapManager.java
浏览文件 @
b2d88ba8
...
...
@@ -27,13 +27,14 @@ import javax.servlet.http.HttpSession;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.servlet.FlashMap
;
import
org.springframework.web.servlet.FlashMapManager
;
import
org.springframework.web.util.UrlPathHelper
;
/**
* A default {@link FlashMapManager} implementation
keep
s {@link FlashMap}
* A default {@link FlashMapManager} implementation
that store
s {@link FlashMap}
* instances in the HTTP session.
*
* @author Rossen Stoyanchev
...
...
@@ -122,9 +123,10 @@ public class DefaultFlashMapManager implements FlashMapManager {
return
false
;
}
}
if
(
flashMap
.
getTargetRequestParams
()
!=
null
)
{
for
(
String
paramName
:
flashMap
.
getTargetRequestParams
().
keySet
())
{
if
(!
flashMap
.
getTargetRequestParams
().
get
(
paramName
).
equals
(
request
.
getParameter
(
paramName
)))
{
MultiValueMap
<
String
,
String
>
params
=
flashMap
.
getTargetRequestParams
();
for
(
String
key
:
params
.
keySet
())
{
for
(
String
value
:
params
.
get
(
key
))
{
if
(!
value
.
equals
(
request
.
getParameter
(
key
)))
{
return
false
;
}
}
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/RedirectView.java
浏览文件 @
b2d88ba8
...
...
@@ -43,6 +43,8 @@ import org.springframework.web.servlet.HandlerMapping;
import
org.springframework.web.servlet.SmartView
;
import
org.springframework.web.servlet.View
;
import
org.springframework.web.servlet.support.RequestContextUtils
;
import
org.springframework.web.util.UriComponents
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
org.springframework.web.util.UriUtils
;
import
org.springframework.web.util.WebUtils
;
...
...
@@ -240,11 +242,9 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView {
FlashMap
flashMap
=
RequestContextUtils
.
getOutputFlashMap
(
request
);
if
(!
CollectionUtils
.
isEmpty
(
flashMap
))
{
String
targetPath
=
WebUtils
.
extractUrlPath
(
targetUrl
.
toString
());
flashMap
.
setTargetRequestPath
(
targetPath
);
if
(
this
.
exposeModelAttributes
)
{
flashMap
.
addTargetRequestParams
(
model
);
}
UriComponents
uriComponents
=
UriComponentsBuilder
.
fromUriString
(
targetUrl
).
build
();
flashMap
.
setTargetRequestPath
(
uriComponents
.
getPath
());
flashMap
.
addTargetRequestParams
(
uriComponents
.
getQueryParams
());
}
sendRedirect
(
request
,
response
,
targetUrl
.
toString
(),
this
.
http10Compatible
);
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/FlashMapTests.java
浏览文件 @
b2d88ba8
...
...
@@ -21,6 +21,8 @@ import static org.junit.Assert.assertFalse;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
org.junit.Test
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
/**
* Test fixture for {@link FlashMap} tests.
...
...
@@ -31,18 +33,17 @@ public class FlashMapTests {
@Test
public
void
isExpired
()
throws
InterruptedException
{
assertFalse
(
new
FlashMap
().
isExpired
());
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
startExpirationPeriod
(
0
);
Thread
.
sleep
(
1
);
Thread
.
sleep
(
100
);
assertTrue
(
flashMap
.
isExpired
());
}
@Test
public
void
notExpired
()
throws
InterruptedException
{
assertFalse
(
new
FlashMap
().
isExpired
());
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
startExpirationPeriod
(
10
);
Thread
.
sleep
(
100
);
...
...
@@ -71,4 +72,51 @@ public class FlashMapTests {
assertEquals
(
0
,
flashMap1
.
compareTo
(
flashMap2
));
}
@Test
public
void
addTargetRequestParamNullValue
()
{
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
addTargetRequestParam
(
"text"
,
"abc"
);
flashMap
.
addTargetRequestParam
(
"empty"
,
" "
);
flashMap
.
addTargetRequestParam
(
"null"
,
null
);
assertEquals
(
1
,
flashMap
.
getTargetRequestParams
().
size
());
assertEquals
(
"abc"
,
flashMap
.
getTargetRequestParams
().
getFirst
(
"text"
));
}
@Test
public
void
addTargetRequestParamsNullValue
()
{
MultiValueMap
<
String
,
String
>
params
=
new
LinkedMultiValueMap
<
String
,
String
>();
params
.
add
(
"key"
,
"abc"
);
params
.
add
(
"key"
,
" "
);
params
.
add
(
"key"
,
null
);
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
addTargetRequestParams
(
params
);
assertEquals
(
1
,
flashMap
.
getTargetRequestParams
().
size
());
assertEquals
(
1
,
flashMap
.
getTargetRequestParams
().
get
(
"key"
).
size
());
assertEquals
(
"abc"
,
flashMap
.
getTargetRequestParams
().
getFirst
(
"key"
));
}
@Test
public
void
addTargetRequestParamNullKey
()
{
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
addTargetRequestParam
(
" "
,
"abc"
);
flashMap
.
addTargetRequestParam
(
null
,
"abc"
);
assertTrue
(
flashMap
.
getTargetRequestParams
().
isEmpty
());
}
@Test
public
void
addTargetRequestParamsNullKey
()
{
MultiValueMap
<
String
,
String
>
params
=
new
LinkedMultiValueMap
<
String
,
String
>();
params
.
add
(
" "
,
"abc"
);
params
.
add
(
null
,
" "
);
FlashMap
flashMap
=
new
FlashMap
();
flashMap
.
addTargetRequestParams
(
params
);
assertTrue
(
flashMap
.
getTargetRequestParams
().
isEmpty
());
}
}
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParserTests.java
浏览文件 @
b2d88ba8
...
...
@@ -67,6 +67,7 @@ public class AnnotationDrivenBeanDefinitionParserTests {
MessageCodesResolver
resolver
=
((
ConfigurableWebBindingInitializer
)
initializer
).
getMessageCodesResolver
();
assertNotNull
(
resolver
);
assertEquals
(
TestMessageCodesResolver
.
class
,
resolver
.
getClass
());
assertEquals
(
true
,
new
DirectFieldAccessor
(
adapter
).
getPropertyValue
(
"ignoreDefaultModelOnRedirect"
));
}
@Test
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java
浏览文件 @
b2d88ba8
...
...
@@ -150,6 +150,8 @@ public class WebMvcConfigurationSupportTests {
Validator
validator
=
initializer
.
getValidator
();
assertNotNull
(
validator
);
assertTrue
(
validator
instanceof
LocalValidatorFactoryBean
);
assertEquals
(
true
,
new
DirectFieldAccessor
(
adapter
).
getPropertyValue
(
"ignoreDefaultModelOnRedirect"
));
}
@Test
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java
浏览文件 @
b2d88ba8
...
...
@@ -103,7 +103,7 @@ public class RequestMappingHandlerAdapterTests {
handlerAdapter
.
setArgumentResolvers
(
Arrays
.
asList
(
redirectAttributesResolver
,
modelResolver
));
handlerAdapter
.
setReturnValueHandlers
(
Arrays
.
asList
(
viewHandler
));
handlerAdapter
.
set
AlwaysUseRedirectAttributes
(
true
);
handlerAdapter
.
set
IgnoreDefaultModelOnRedirect
(
true
);
handlerAdapter
.
afterPropertiesSet
();
request
.
setAttribute
(
FlashMapManager
.
OUTPUT_FLASH_MAP_ATTRIBUTE
,
new
FlashMap
());
...
...
@@ -111,7 +111,7 @@ public class RequestMappingHandlerAdapterTests {
HandlerMethod
handlerMethod
=
handlerMethod
(
new
RedirectAttributeHandler
(),
"handle"
,
Model
.
class
);
ModelAndView
mav
=
handlerAdapter
.
handle
(
request
,
response
,
handlerMethod
);
assertTrue
(
"
No redirect attributes added
, model should be empty"
,
mav
.
getModel
().
isEmpty
());
assertTrue
(
"
Without RedirectAttributes arg
, model should be empty"
,
mav
.
getModel
().
isEmpty
());
}
@Test
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java
浏览文件 @
b2d88ba8
...
...
@@ -71,8 +71,8 @@ public class ServletInvocableHandlerMethodTests {
ServletInvocableHandlerMethod
handlerMethod
=
getHandlerMethod
(
"responseStatus"
);
handlerMethod
.
invokeAndHandle
(
webRequest
,
mavContainer
);
assert
False
(
"Null return value + @ResponseStatus should result in 'no view resolution
'"
,
mavContainer
.
isRe
solveView
());
assert
True
(
"Null return value + @ResponseStatus should result in 'request handled
'"
,
mavContainer
.
isRe
questHandled
());
assertEquals
(
HttpStatus
.
BAD_REQUEST
.
value
(),
response
.
getStatus
());
assertEquals
(
"400 Bad Request"
,
response
.
getErrorMessage
());
...
...
@@ -85,8 +85,8 @@ public class ServletInvocableHandlerMethodTests {
ServletInvocableHandlerMethod
handlerMethod
=
getHandlerMethod
(
"httpServletResponse"
,
HttpServletResponse
.
class
);
handlerMethod
.
invokeAndHandle
(
webRequest
,
mavContainer
);
assert
False
(
"Null return value + HttpServletResponse arg should result in 'no view resolution
'"
,
mavContainer
.
isRe
solveView
());
assert
True
(
"Null return value + HttpServletResponse arg should result in 'request handled
'"
,
mavContainer
.
isRe
questHandled
());
}
@Test
...
...
@@ -98,8 +98,8 @@ public class ServletInvocableHandlerMethodTests {
ServletInvocableHandlerMethod
handlerMethod
=
getHandlerMethod
(
"notModified"
);
handlerMethod
.
invokeAndHandle
(
webRequest
,
mavContainer
);
assert
False
(
"Null return value + 'not modified' request should result in 'no view resolution
'"
,
mavContainer
.
isRe
solveView
());
assert
True
(
"Null return value + 'not modified' request should result in 'request handled
'"
,
mavContainer
.
isRe
questHandled
());
}
@Test
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/DefaultMethodReturnValueHandlerTests.java
浏览文件 @
b2d88ba8
...
...
@@ -17,6 +17,7 @@
package
org.springframework.web.servlet.mvc.method.annotation.support
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
...
@@ -70,7 +71,7 @@ public class DefaultMethodReturnValueHandlerTests {
assertEquals
(
"viewName"
,
mavContainer
.
getViewName
());
assertSame
(
testBean
,
mavContainer
.
getModel
().
get
(
"modelAttrName"
));
assert
True
(
mavContainer
.
isResolveView
());
assert
False
(
mavContainer
.
isRequestHandled
());
}
@Test
(
expected
=
UnsupportedOperationException
.
class
)
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/HttpEntityMethodProcessorTests.java
浏览文件 @
b2d88ba8
...
...
@@ -138,7 +138,7 @@ public class HttpEntityMethodProcessorTests {
Object
result
=
processor
.
resolveArgument
(
paramHttpEntity
,
mavContainer
,
webRequest
,
null
);
assertTrue
(
result
instanceof
HttpEntity
);
assert
True
(
"The ResolveView flag shouldn't change"
,
mavContainer
.
isResolveView
());
assert
False
(
"The requestHandled flag shouldn't change"
,
mavContainer
.
isRequestHandled
());
assertEquals
(
"Invalid argument"
,
body
,
((
HttpEntity
<?>)
result
).
getBody
());
verify
(
messageConverter
);
}
...
...
@@ -179,7 +179,7 @@ public class HttpEntityMethodProcessorTests {
processor
.
handleReturnValue
(
returnValue
,
returnTypeResponseEntity
,
mavContainer
,
webRequest
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
verify
(
messageConverter
);
}
...
...
@@ -197,7 +197,7 @@ public class HttpEntityMethodProcessorTests {
processor
.
handleReturnValue
(
returnValue
,
returnTypeResponseEntityProduces
,
mavContainer
,
webRequest
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
verify
(
messageConverter
);
}
...
...
@@ -245,7 +245,7 @@ public class HttpEntityMethodProcessorTests {
processor
.
handleReturnValue
(
returnValue
,
returnTypeResponseEntity
,
mavContainer
,
webRequest
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
assertEquals
(
"headerValue"
,
servletResponse
.
getHeader
(
"headerName"
));
}
...
...
@@ -264,7 +264,7 @@ public class HttpEntityMethodProcessorTests {
processor
.
handleReturnValue
(
returnValue
,
returnTypeResponseEntity
,
mavContainer
,
webRequest
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
assertEquals
(
"headerValue"
,
outputMessage
.
getValue
().
getHeaders
().
get
(
"header"
).
get
(
0
));
verify
(
messageConverter
);
}
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ModelAndViewMethodReturnValueHandlerTests.java
浏览文件 @
b2d88ba8
...
...
@@ -79,7 +79,7 @@ public class ModelAndViewMethodReturnValueHandlerTests {
public
void
handleReturnValueNull
()
throws
Exception
{
handler
.
handleReturnValue
(
null
,
getReturnValueParam
(
"modelAndView"
),
mavContainer
,
webRequest
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
}
private
MethodParameter
getReturnValueParam
(
String
methodName
)
throws
Exception
{
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestPartMethodArgumentResolverTests.java
浏览文件 @
b2d88ba8
...
...
@@ -245,7 +245,7 @@ public class RequestPartMethodArgumentResolverTests {
Object
actualValue
=
resolver
.
resolveArgument
(
parameter
,
mavContainer
,
webRequest
,
new
ValidatingBinderFactory
());
assertEquals
(
"Invalid argument value"
,
argValue
,
actualValue
);
assert
True
(
"The ResolveView flag shouldn't change"
,
mavContainer
.
isResolveView
());
assert
False
(
"The requestHandled flag shouldn't change"
,
mavContainer
.
isRequestHandled
());
verify
(
messageConverter
);
}
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/RequestResponseBodyMethodProcessorTests.java
浏览文件 @
b2d88ba8
...
...
@@ -140,7 +140,7 @@ public class RequestResponseBodyMethodProcessorTests {
Object
result
=
processor
.
resolveArgument
(
paramRequestBodyString
,
mavContainer
,
webRequest
,
null
);
assertEquals
(
"Invalid argument"
,
body
,
result
);
assert
True
(
"The ResolveView flag shouldn't change"
,
mavContainer
.
isResolveView
());
assert
False
(
"The requestHandled flag shouldn't change"
,
mavContainer
.
isRequestHandled
());
verify
(
messageConverter
);
}
...
...
@@ -211,7 +211,7 @@ public class RequestResponseBodyMethodProcessorTests {
processor
.
handleReturnValue
(
body
,
returnTypeString
,
mavContainer
,
webRequest
);
assert
False
(
"The ResolveView flag wasn't turned off"
,
mavContainer
.
isResolveView
());
assert
True
(
"The requestHandled flag wasn't set"
,
mavContainer
.
isRequestHandled
());
verify
(
messageConverter
);
}
...
...
@@ -228,7 +228,7 @@ public class RequestResponseBodyMethodProcessorTests {
processor
.
handleReturnValue
(
body
,
returnTypeStringProduces
,
mavContainer
,
webRequest
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
verify
(
messageConverter
);
}
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletRequestMethodArgumentResolverTests.java
浏览文件 @
b2d88ba8
...
...
@@ -75,7 +75,7 @@ public class ServletRequestMethodArgumentResolverTests {
assertTrue
(
"ServletRequest not supported"
,
isSupported
);
assertSame
(
"Invalid result"
,
servletRequest
,
result
);
assert
True
(
"The ResolveView flag shouldn't change"
,
mavContainer
.
isResolveView
());
assert
False
(
"The requestHandled flag shouldn't change"
,
mavContainer
.
isRequestHandled
());
}
@Test
...
...
@@ -89,7 +89,7 @@ public class ServletRequestMethodArgumentResolverTests {
assertTrue
(
"Session not supported"
,
isSupported
);
assertSame
(
"Invalid result"
,
session
,
result
);
assert
True
(
"The ResolveView flag shouldn't change"
,
mavContainer
.
isResolveView
());
assert
False
(
"The requestHandled flag shouldn't change"
,
mavContainer
.
isRequestHandled
());
}
@Test
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/method/annotation/support/ServletResponseMethodArgumentResolverTests.java
浏览文件 @
b2d88ba8
...
...
@@ -67,7 +67,7 @@ public class ServletResponseMethodArgumentResolverTests {
Object
result
=
resolver
.
resolveArgument
(
servletResponseParameter
,
mavContainer
,
webRequest
,
null
);
assertSame
(
"Invalid result"
,
servletResponse
,
result
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
}
@Test
...
...
@@ -78,7 +78,7 @@ public class ServletResponseMethodArgumentResolverTests {
Object
result
=
resolver
.
resolveArgument
(
outputStreamParameter
,
mavContainer
,
webRequest
,
null
);
assertSame
(
"Invalid result"
,
servletResponse
.
getOutputStream
(),
result
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
}
@Test
...
...
@@ -89,7 +89,7 @@ public class ServletResponseMethodArgumentResolverTests {
Object
result
=
resolver
.
resolveArgument
(
writerParameter
,
mavContainer
,
webRequest
,
null
);
assertSame
(
"Invalid result"
,
servletResponse
.
getWriter
(),
result
);
assert
False
(
mavContainer
.
isResolveView
());
assert
True
(
mavContainer
.
isRequestHandled
());
}
public
void
supportedParams
(
ServletResponse
p0
,
OutputStream
p1
,
Writer
p2
)
{
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/support/DefaultFlashMapManagerTests.java
浏览文件 @
b2d88ba8
...
...
@@ -163,9 +163,7 @@ public class DefaultFlashMapManagerTests {
allMaps
.
add
(
flashMap
);
flashMap
.
startExpirationPeriod
(
0
);
}
Thread
.
sleep
(
5
);
Thread
.
sleep
(
100
);
this
.
flashMapManager
.
requestStarted
(
this
.
request
);
assertEquals
(
0
,
allMaps
.
size
());
...
...
@@ -197,7 +195,7 @@ public class DefaultFlashMapManagerTests {
this
.
flashMapManager
.
setFlashMapTimeout
(
0
);
this
.
flashMapManager
.
requestCompleted
(
this
.
request
);
Thread
.
sleep
(
1
);
Thread
.
sleep
(
1
00
);
List
<
FlashMap
>
allMaps
=
getFlashMaps
();
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/RedirectViewTests.java
浏览文件 @
b2d88ba8
...
...
@@ -129,7 +129,7 @@ public class RedirectViewTests {
assertEquals
(
"http://url.somewhere.com/path?id=1"
,
response
.
getHeader
(
"Location"
));
assertEquals
(
"/path"
,
flashMap
.
getTargetRequestPath
());
assertEquals
(
model
,
flashMap
.
getTargetRequestParams
());
assertEquals
(
model
,
flashMap
.
getTargetRequestParams
()
.
toSingleValueMap
()
);
}
@Test
...
...
org.springframework.web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java
浏览文件 @
b2d88ba8
...
...
@@ -211,7 +211,7 @@ public final class ModelFactory {
this
.
sessionAttributesHandler
.
storeAttributes
(
request
,
mavContainer
.
getModel
());
}
if
(
mavContainer
.
isResolveView
())
{
if
(
!
mavContainer
.
isRequestHandled
())
{
updateBindingResult
(
request
,
mavContainer
.
getModel
());
}
}
...
...
org.springframework.web/src/main/java/org/springframework/web/method/support/HandlerMethodArgumentResolver.java
浏览文件 @
b2d88ba8
...
...
@@ -22,7 +22,8 @@ import org.springframework.web.bind.support.WebDataBinderFactory;
import
org.springframework.web.context.request.NativeWebRequest
;
/**
* Strategy interface for resolving method parameters into argument values in the context of a given request.
* Strategy interface for resolving method parameters into argument values in
* the context of a given request.
*
* @author Arjen Poutsma
* @since 3.1
...
...
@@ -30,21 +31,27 @@ import org.springframework.web.context.request.NativeWebRequest;
public
interface
HandlerMethodArgumentResolver
{
/**
* Whether the given {@linkplain MethodParameter method parameter} is supported by this resolver.
* Whether the given {@linkplain MethodParameter method parameter} is
* supported by this resolver.
*
* @param parameter the method parameter to check
* @return {@code true} if this resolver supports the supplied parameter; {@code false} otherwise
* @return {@code true} if this resolver supports the supplied parameter;
* {@code false} otherwise
*/
boolean
supportsParameter
(
MethodParameter
parameter
);
/**
* Resolves a method parameter into an argument value from a given request. A {@link ModelAndViewContainer}
* provides access to the model for the request. A {@link WebDataBinderFactory} provides a way to create
* a {@link WebDataBinder} instance when needed for data binding and type conversion purposes.
* Resolves a method parameter into an argument value from a given request.
* A {@link ModelAndViewContainer} provides access to the model for the
* request. A {@link WebDataBinderFactory} provides a way to create
* a {@link WebDataBinder} instance when needed for data binding and
* type conversion purposes.
*
* @param parameter the method parameter to resolve. This parameter must have previously been passed to
* {@link #supportsParameter(org.springframework.core.MethodParameter)} and it must have returned {@code true}
* @param mavContainer the {@link ModelAndViewContainer} for the current request
* @param parameter the method parameter to resolve. This parameter must
* have previously been passed to
* {@link #supportsParameter(org.springframework.core.MethodParameter)}
* and it must have returned {@code true}
* @param mavContainer the ModelAndViewContainer for the current request
* @param webRequest the current request
* @param binderFactory a factory for creating {@link WebDataBinder} instances
* @return the resolved argument value, or {@code null}.
...
...
org.springframework.web/src/main/java/org/springframework/web/method/support/HandlerMethodReturnValueHandler.java
浏览文件 @
b2d88ba8
...
...
@@ -20,7 +20,8 @@ import org.springframework.core.MethodParameter;
import
org.springframework.web.context.request.NativeWebRequest
;
/**
* Strategy interface to handle the value returned from the invocation of a handler method .
* Strategy interface to handle the value returned from the invocation of a
* handler method .
*
* @author Arjen Poutsma
* @since 3.1
...
...
@@ -28,24 +29,27 @@ import org.springframework.web.context.request.NativeWebRequest;
public
interface
HandlerMethodReturnValueHandler
{
/**
* Whether the given {@linkplain MethodParameter method return type} is supported by this handler.
* Whether the given {@linkplain MethodParameter method return type} is
* supported by this handler.
*
* @param returnType the method return type to check
* @return {@code true} if this handler supports the supplied return type; {@code false} otherwise
* @return {@code true} if this handler supports the supplied return type;
* {@code false} otherwise
*/
boolean
supportsReturnType
(
MethodParameter
returnType
);
/**
* Handle the given return value by adding attributes to the model, setting the view (or view name),
* or by writing to the response. {@link HandlerMethodReturnValueHandler} implementations should also
* consider whether to set {@link ModelAndViewContainer#setResolveView(boolean)}, which is set to
* {@code true} by default and therefore needs to be set to {@code false} explicitly if view
* resolution is to be bypassed.
* Handle the given return value by adding attributes to the model and
* setting a view or setting the
* {@link ModelAndViewContainer#setRequestHandled} flag to {@code true}
* to indicate the response has been handled directly.
*
* @param returnValue the value returned from the handler method
* @param returnType the type of the return value. This type must have previously been passed to
* {@link #supportsReturnType(org.springframework.core.MethodParameter)} and it must have returned {@code true}
* @param mavContainer the {@link ModelAndViewContainer} for the current request
* @param returnType the type of the return value. This type must have
* previously been passed to
* {@link #supportsReturnType(org.springframework.core.MethodParameter)}
* and it must have returned {@code true}
* @param mavContainer the ModelAndViewContainer for the current request
* @param webRequest the current request
* @throws Exception if the return value handling results in an error
*/
...
...
org.springframework.web/src/main/java/org/springframework/web/method/support/ModelAndViewContainer.java
浏览文件 @
b2d88ba8
...
...
@@ -28,14 +28,14 @@ import org.springframework.validation.support.BindingAwareModelMap;
* {@link HandlerMethodReturnValueHandler}s during the course of invocation of
* a controller method.
*
* <p>The {@link #setRe
solveView(boolean)} flag can be used to indicate tha
t
*
view resolution is not required (e.g. {@code @ResponseBody} method)
.
* <p>The {@link #setRe
questHandled} flag can be used to indicate the reques
t
*
has been handled directly and view resolution is not required
.
*
* <p>A default {@link Model} is
created at instantiation and used thereafter
.
*
The {@link #setRedirectModel(ModelMap)} method can be used to provide a
*
separate model to use potentially in case of a redirect.
*
The {@link #setUseRedirectModel()} can be used to enable use of the
* re
direct model if the controller decides to redirect.
* <p>A default {@link Model} is
automatically created at instantiation
.
*
An alternate model instance may be provided via {@link #setRedirectModel}
*
for use in a redirect scenario. When {@link #setUseRedirectModel} is set
*
to {@code true} signalling a redirect scenario, the {@link #getModel()}
* re
turns the redirect model instead of the default model.
*
* @author Rossen Stoyanchev
* @since 3.1
...
...
@@ -44,12 +44,14 @@ public class ModelAndViewContainer {
private
Object
view
;
private
boolean
re
solveView
=
tru
e
;
private
boolean
re
questHandled
=
fals
e
;
private
final
ModelMap
model
=
new
BindingAwareModelMap
();
private
ModelMap
redirectModel
;
private
boolean
ignoreDefaultModelOnRedirect
=
false
;
private
boolean
useRedirectModel
=
false
;
/**
...
...
@@ -99,7 +101,7 @@ public class ModelAndViewContainer {
}
/**
*
Whether view resolution is required or not
.
*
Signal a scenario where the request is handled directly
.
* <p>A {@link HandlerMethodReturnValueHandler} may use this flag to
* indicate the response has been fully handled and view resolution
* is not required (e.g. {@code @ResponseBody}).
...
...
@@ -109,54 +111,64 @@ public class ModelAndViewContainer {
* a complete response depending on the method return value.
* <p>The default value is {@code true}.
*/
public
void
setRe
solveView
(
boolean
resolveView
)
{
this
.
re
solveView
=
resolveView
;
public
void
setRe
questHandled
(
boolean
requestHandled
)
{
this
.
re
questHandled
=
requestHandled
;
}
/**
* Whether
view resolution is required or not
.
* Whether
the request is handled directly
.
*/
public
boolean
isRe
solveView
()
{
return
this
.
re
solveView
;
public
boolean
isRe
questHandled
()
{
return
this
.
re
questHandled
;
}
/**
* Return the default model created at instantiation or the one provided
* via {@link #setRedirectModel(ModelMap)} as long as it has been enabled
* via {@link #setUseRedirectModel()}.
* Return the model to use. This is either the default model created at
* instantiation or the redirect model if {@link #setUseRedirectModel}
* is set to {@code true}. If a redirect model was never provided via
* {@link #setRedirectModel}, return the default model unless
* {@link #setIgnoreDefaultModelOnRedirect} is set to {@code true}.
*/
public
ModelMap
getModel
()
{
if
((
this
.
redirectModel
!=
null
)
&&
this
.
useRedirectModel
)
{
if
(!
this
.
useRedirectModel
)
{
return
this
.
model
;
}
else
if
(
this
.
redirectModel
!=
null
)
{
return
this
.
redirectModel
;
}
else
{
return
this
.
model
;
return
this
.
ignoreDefaultModelOnRedirect
?
new
ModelMap
()
:
this
.
model
;
}
}
/**
* Provide a model instance to use in case the controller redirects.
* Note that {@link #setUseRedirectModel()} must also be called in order
* to enable use of the redirect model.
* Provide a separate model instance to use in a redirect scenario.
* The provided additional model however is not used used unless
* {@link #setUseRedirectModel(boolean)} gets set to {@code true} to signal
* a redirect scenario.
*/
public
void
setRedirectModel
(
ModelMap
redirectModel
)
{
this
.
redirectModel
=
redirectModel
;
}
/**
* Return the redirect model provided via
* {@link #setRedirectModel(ModelMap)} or {@code null} if not provided.
* When set to {@code true} the default model is never used in a redirect
* scenario. So if a redirect model is not available, an empty model is
* used instead.
* <p>When set to {@code false} the default model can be used in a redirect
* scenario if a redirect model is not available.
* <p>The default setting is {@code false}.
*/
public
ModelMap
getRedirectModel
(
)
{
return
this
.
redirectModel
;
public
void
setIgnoreDefaultModelOnRedirect
(
boolean
ignoreDefaultModelOnRedirect
)
{
this
.
ignoreDefaultModelOnRedirect
=
ignoreDefaultModelOnRedirect
;
}
/**
*
Indicate that the redirect model provided via
*
{@link #setRedirectModel(ModelMap)} should be used
.
*
Signal the conditions for using a redirect model are in place -- e.g.
*
the controller has requested a redirect
.
*/
public
void
setUseRedirectModel
()
{
this
.
useRedirectModel
=
true
;
public
void
setUseRedirectModel
(
boolean
useRedirectModel
)
{
this
.
useRedirectModel
=
useRedirectModel
;
}
/**
...
...
@@ -210,7 +222,7 @@ public class ModelAndViewContainer {
@Override
public
String
toString
()
{
StringBuilder
sb
=
new
StringBuilder
(
"ModelAndViewContainer: "
);
if
(
isResolveView
())
{
if
(
!
isRequestHandled
())
{
if
(
isViewReference
())
{
sb
.
append
(
"reference to view with name '"
).
append
(
this
.
view
).
append
(
"'"
);
}
...
...
@@ -220,7 +232,7 @@ public class ModelAndViewContainer {
sb
.
append
(
"; model is "
).
append
(
getModel
());
}
else
{
sb
.
append
(
"
View resolution not required
"
);
sb
.
append
(
"
Request handled directly
"
);
}
return
sb
.
toString
();
}
...
...
org.springframework.web/src/main/java/org/springframework/web/util/WebUtils.java
浏览文件 @
b2d88ba8
...
...
@@ -724,29 +724,4 @@ public abstract class WebUtils {
return
urlPath
.
substring
(
begin
,
end
);
}
/**
* Extracts the path from the given URL by removing the query at the end
* and the scheme and authority in the front, if present.
* @param url a URL, never {@code null}
* @return the extracted URL path
*/
public
static
String
extractUrlPath
(
String
url
)
{
// Remove query/fragment
int
end
=
url
.
indexOf
(
'?'
);
if
(
end
==
-
1
)
{
end
=
url
.
indexOf
(
'#'
);
if
(
end
==
-
1
)
{
end
=
url
.
length
();
}
}
url
=
url
.
substring
(
0
,
end
);
// Remove scheme + authority
int
start
=
url
.
indexOf
(
"://"
);
if
(
start
!=
-
1
)
{
start
=
url
.
indexOf
(
'/'
,
start
+
3
);
url
=
(
start
!=
-
1
)
?
url
.
substring
(
start
)
:
""
;
}
return
url
;
}
}
org.springframework.web/src/test/java/org/springframework/web/method/support/ModelAndViewContainerTests.java
0 → 100644
浏览文件 @
b2d88ba8
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.web.method.support
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.ui.ModelMap
;
/**
* Test fixture for {@link ModelAndViewContainer}.
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public
class
ModelAndViewContainerTests
{
private
ModelAndViewContainer
mavContainer
;
@Before
public
void
setup
()
{
this
.
mavContainer
=
new
ModelAndViewContainer
();
}
@Test
public
void
getModel
()
{
this
.
mavContainer
.
addAttribute
(
"name"
,
"value"
);
assertEquals
(
1
,
this
.
mavContainer
.
getModel
().
size
());
}
@Test
public
void
getModelRedirectModel
()
{
ModelMap
redirectModel
=
new
ModelMap
(
"name"
,
"redirectValue"
);
this
.
mavContainer
.
setRedirectModel
(
redirectModel
);
this
.
mavContainer
.
addAttribute
(
"name"
,
"value"
);
assertEquals
(
"Default model should be used if not in redirect scenario"
,
"value"
,
this
.
mavContainer
.
getModel
().
get
(
"name"
));
this
.
mavContainer
.
setUseRedirectModel
(
true
);
assertEquals
(
"Redirect model should be used in redirect scenario"
,
"redirectValue"
,
this
.
mavContainer
.
getModel
().
get
(
"name"
));
}
@Test
public
void
getModelIgnoreDefaultModelOnRedirect
()
{
this
.
mavContainer
.
addAttribute
(
"name"
,
"value"
);
this
.
mavContainer
.
setUseRedirectModel
(
true
);
assertEquals
(
"Default model should be used since no redirect model was provided"
,
1
,
this
.
mavContainer
.
getModel
().
size
());
this
.
mavContainer
.
setIgnoreDefaultModelOnRedirect
(
true
);
assertEquals
(
"Empty model should be returned if no redirect model is available"
,
0
,
this
.
mavContainer
.
getModel
().
size
());
}
}
org.springframework.web/src/test/java/org/springframework/web/util/WebUtilsTests.java
浏览文件 @
b2d88ba8
...
...
@@ -64,15 +64,4 @@ public class WebUtilsTests {
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html?param=/path/a.do"
));
}
@Test
public
void
extractUriPath
()
{
assertEquals
(
""
,
WebUtils
.
extractUrlPath
(
"http://example.com"
));
assertEquals
(
"/"
,
WebUtils
.
extractUrlPath
(
"http://example.com/"
));
assertEquals
(
"/rfc/rfc3986.txt"
,
WebUtils
.
extractUrlPath
(
"http://www.ietf.org/rfc/rfc3986.txt"
));
assertEquals
(
"/over/there"
,
WebUtils
.
extractUrlPath
(
"http://example.com/over/there?name=ferret#nose"
));
assertEquals
(
"/over/there"
,
WebUtils
.
extractUrlPath
(
"http://example.com/over/there#nose"
));
assertEquals
(
"/over/there"
,
WebUtils
.
extractUrlPath
(
"/over/there?name=ferret#nose"
));
assertEquals
(
"/over/there"
,
WebUtils
.
extractUrlPath
(
"/over/there?url=http://example.com"
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录