Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
153a23db
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 搜索 >>
提交
153a23db
编写于
12月 04, 2015
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Configure view controllers with ApplicationContext
Issue: SPR-13762
上级
3d1ae9c6
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
39 addition
and
10 deletion
+39
-10
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/RedirectViewControllerRegistration.java
...config/annotation/RedirectViewControllerRegistration.java
+6
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistration.java
...servlet/config/annotation/ViewControllerRegistration.java
+5
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java
...web/servlet/config/annotation/ViewControllerRegistry.java
+11
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java
...servlet/config/annotation/WebMvcConfigurationSupport.java
+1
-0
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistryTests.java
...ervlet/config/annotation/ViewControllerRegistryTests.java
+16
-7
未找到文件。
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/RedirectViewControllerRegistration.java
浏览文件 @
153a23db
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -16,6 +16,7 @@
package
org.springframework.web.servlet.config.annotation
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.util.Assert
;
import
org.springframework.web.servlet.mvc.ParameterizableViewController
;
...
...
@@ -81,6 +82,10 @@ public class RedirectViewControllerRegistration {
return
this
;
}
protected
void
setApplicationContext
(
ApplicationContext
applicationContext
)
{
this
.
controller
.
setApplicationContext
(
applicationContext
);
this
.
redirectView
.
setApplicationContext
(
applicationContext
);
}
protected
String
getUrlPath
()
{
return
this
.
urlPath
;
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistration.java
浏览文件 @
153a23db
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -16,6 +16,7 @@
package
org.springframework.web.servlet.config.annotation
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.util.Assert
;
import
org.springframework.web.servlet.RequestToViewNameTranslator
;
...
...
@@ -65,6 +66,9 @@ public class ViewControllerRegistration {
this
.
controller
.
setViewName
(
viewName
);
}
protected
void
setApplicationContext
(
ApplicationContext
applicationContext
)
{
this
.
controller
.
setApplicationContext
(
applicationContext
);
}
protected
String
getUrlPath
()
{
return
this
.
urlPath
;
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java
浏览文件 @
153a23db
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -21,6 +21,7 @@ import java.util.LinkedHashMap;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.servlet.handler.AbstractHandlerMapping
;
import
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
;
...
...
@@ -42,6 +43,8 @@ public class ViewControllerRegistry {
private
int
order
=
1
;
private
ApplicationContext
applicationContext
;
/**
* Map a view controller to the given URL path (or pattern) in order to render
...
...
@@ -49,6 +52,7 @@ public class ViewControllerRegistry {
*/
public
ViewControllerRegistration
addViewController
(
String
urlPath
)
{
ViewControllerRegistration
registration
=
new
ViewControllerRegistration
(
urlPath
);
registration
.
setApplicationContext
(
this
.
applicationContext
);
this
.
registrations
.
add
(
registration
);
return
registration
;
}
...
...
@@ -61,6 +65,7 @@ public class ViewControllerRegistry {
*/
public
RedirectViewControllerRegistration
addRedirectViewController
(
String
urlPath
,
String
redirectUrl
)
{
RedirectViewControllerRegistration
registration
=
new
RedirectViewControllerRegistration
(
urlPath
,
redirectUrl
);
registration
.
setApplicationContext
(
this
.
applicationContext
);
this
.
redirectRegistrations
.
add
(
registration
);
return
registration
;
}
...
...
@@ -72,6 +77,7 @@ public class ViewControllerRegistry {
*/
public
void
addStatusController
(
String
urlPath
,
HttpStatus
statusCode
)
{
ViewControllerRegistration
registration
=
new
ViewControllerRegistration
(
urlPath
);
registration
.
setApplicationContext
(
this
.
applicationContext
);
registration
.
setStatusCode
(
statusCode
);
registration
.
getViewController
().
setStatusOnly
(
true
);
this
.
registrations
.
add
(
registration
);
...
...
@@ -87,6 +93,10 @@ public class ViewControllerRegistry {
this
.
order
=
order
;
}
protected
void
setApplicationContext
(
ApplicationContext
applicationContext
)
{
this
.
applicationContext
=
applicationContext
;
}
/**
* Return the {@code HandlerMapping} that contains the registered view
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java
浏览文件 @
153a23db
...
...
@@ -364,6 +364,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
@Bean
public
HandlerMapping
viewControllerHandlerMapping
()
{
ViewControllerRegistry
registry
=
new
ViewControllerRegistry
();
registry
.
setApplicationContext
(
this
.
applicationContext
);
addViewControllers
(
registry
);
AbstractHandlerMapping
handlerMapping
=
registry
.
getHandlerMapping
();
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistryTests.java
浏览文件 @
153a23db
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -22,6 +22,7 @@ import java.util.Map;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.context.support.StaticApplicationContext
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.mock.web.test.MockHttpServletRequest
;
import
org.springframework.mock.web.test.MockHttpServletResponse
;
...
...
@@ -48,6 +49,7 @@ public class ViewControllerRegistryTests {
@Before
public
void
setUp
()
{
this
.
registry
=
new
ViewControllerRegistry
();
this
.
registry
.
setApplicationContext
(
new
StaticApplicationContext
());
this
.
request
=
new
MockHttpServletRequest
(
"GET"
,
"/"
);
this
.
response
=
new
MockHttpServletResponse
();
}
...
...
@@ -61,18 +63,22 @@ public class ViewControllerRegistryTests {
public
void
addViewController
()
{
this
.
registry
.
addViewController
(
"/path"
).
setViewName
(
"viewName"
);
ParameterizableViewController
controller
=
getController
(
"/path"
);
assertEquals
(
"viewName"
,
controller
.
getViewName
());
assertNull
(
controller
.
getStatusCode
());
assertFalse
(
controller
.
isStatusOnly
());
assertNotNull
(
controller
.
getApplicationContext
());
}
@Test
public
void
addViewControllerWithDefaultViewName
()
{
this
.
registry
.
addViewController
(
"/path"
);
ParameterizableViewController
controller
=
getController
(
"/path"
);
assertNull
(
controller
.
getViewName
());
assertNull
(
controller
.
getStatusCode
());
assertFalse
(
controller
.
isStatusOnly
());
assertNotNull
(
controller
.
getApplicationContext
());
}
@Test
...
...
@@ -85,6 +91,7 @@ public class ViewControllerRegistryTests {
assertEquals
(
302
,
this
.
response
.
getStatus
());
assertEquals
(
"/context/redirectTo"
,
this
.
response
.
getRedirectedUrl
());
assertNotNull
(
redirectView
.
getApplicationContext
());
}
@Test
...
...
@@ -101,18 +108,20 @@ public class ViewControllerRegistryTests {
assertEquals
(
308
,
this
.
response
.
getStatus
());
assertEquals
(
"/redirectTo?a=b"
,
response
.
getRedirectedUrl
());
assertNotNull
(
redirectView
.
getApplicationContext
());
}
@Test
public
void
addStatusController
()
{
this
.
registry
.
addStatusController
(
"/path"
,
HttpStatus
.
NOT_FOUND
);
ParameterizableViewController
controller
=
getController
(
"/path"
);
assertNull
(
controller
.
getViewName
());
assertEquals
(
HttpStatus
.
NOT_FOUND
,
controller
.
getStatusCode
());
assertTrue
(
controller
.
isStatusOnly
());
assertNotNull
(
controller
.
getApplicationContext
());
}
@Test
public
void
order
()
{
this
.
registry
.
addViewController
(
"/path"
);
...
...
@@ -124,6 +133,7 @@ public class ViewControllerRegistryTests {
assertEquals
(
2
,
handlerMapping
.
getOrder
());
}
private
ParameterizableViewController
getController
(
String
path
)
{
Map
<
String
,
?>
urlMap
=
getHandlerMapping
().
getUrlMap
();
ParameterizableViewController
controller
=
(
ParameterizableViewController
)
urlMap
.
get
(
path
);
...
...
@@ -131,17 +141,16 @@ public class ViewControllerRegistryTests {
return
controller
;
}
private
SimpleUrlHandlerMapping
getHandlerMapping
()
{
return
(
SimpleUrlHandlerMapping
)
this
.
registry
.
getHandlerMapping
();
}
private
RedirectView
getRedirectView
(
String
path
)
{
ParameterizableViewController
controller
=
getController
(
"/path"
);
ParameterizableViewController
controller
=
getController
(
path
);
assertNull
(
controller
.
getViewName
());
assertNotNull
(
controller
.
getView
());
assertEquals
(
RedirectView
.
class
,
controller
.
getView
().
getClass
());
return
(
RedirectView
)
controller
.
getView
();
}
private
SimpleUrlHandlerMapping
getHandlerMapping
()
{
return
(
SimpleUrlHandlerMapping
)
this
.
registry
.
getHandlerMapping
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录