Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
2b1d1cd3
Commit
2b1d1cd3
authored
Nov 21, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Auto-configure templated welcome page"
Closes gh-10545
parent
cc855f44
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
8 deletions
+103
-8
WelcomePageHandlerMapping.java
.../autoconfigure/web/servlet/WelcomePageHandlerMapping.java
+6
-5
WelcomePageHandlerMappingTests.java
...configure/web/servlet/WelcomePageHandlerMappingTests.java
+89
-0
spring-boot-features.adoc
...ing-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+8
-3
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java
View file @
2b1d1cd3
...
...
@@ -48,14 +48,15 @@ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping {
WelcomePageHandlerMapping
(
TemplateAvailabilityProviders
templateAvailabilityProviders
,
ApplicationContext
applicationContext
,
Optional
<
Resource
>
welcomePage
,
String
staticPathPattern
)
{
if
(
welcomeTemplateExists
(
templateAvailabilityProviders
,
applicationContext
))
{
if
(
welcomePage
.
isPresent
()
&&
"/**"
.
equals
(
staticPathPattern
))
{
logger
.
info
(
"Adding welcome page: "
+
welcomePage
.
get
());
setRootViewName
(
"forward:index.html"
);
}
else
if
(
welcomeTemplateExists
(
templateAvailabilityProviders
,
applicationContext
))
{
logger
.
info
(
"Adding welcome page template: index"
);
setRootViewName
(
"index"
);
}
else
if
(
welcomePage
.
isPresent
()
&&
"/**"
.
equals
(
staticPathPattern
))
{
logger
.
info
(
"Adding welcome page: "
+
welcomePage
);
setRootViewName
(
"forward:index.html"
);
}
}
private
boolean
welcomeTemplateExists
(
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java
View file @
2b1d1cd3
...
...
@@ -16,14 +16,21 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
web
.
servlet
;
import
java.util.Collections
;
import
java.util.Map
;
import
java.util.Optional
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.junit.Test
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider
;
import
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders
;
import
org.springframework.boot.test.context.runner.WebApplicationContextRunner
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
...
...
@@ -32,10 +39,15 @@ import org.springframework.core.io.FileSystemResource;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.servlet.ViewResolver
;
import
org.springframework.web.servlet.view.AbstractView
;
import
org.springframework.web.servlet.view.InternalResourceView
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
content
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
forwardedUrl
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
...
...
@@ -109,17 +121,55 @@ public class WelcomePageHandlerMappingTests {
.
andExpect
(
status
().
isNotFound
()));
}
@Test
public
void
handlesRequestForTemplateThatAcceptsTextHtml
()
{
this
.
contextRunner
.
withUserConfiguration
(
TemplateConfiguration
.
class
)
.
run
((
context
)
->
{
MockMvc
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
context
).
build
();
mockMvc
.
perform
(
get
(
"/"
).
accept
(
MediaType
.
TEXT_HTML
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
content
().
string
(
"index template"
));
});
}
@Test
public
void
handlesRequestForTemplateThatAcceptsAll
()
{
this
.
contextRunner
.
withUserConfiguration
(
TemplateConfiguration
.
class
)
.
run
((
context
)
->
{
MockMvc
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
context
).
build
();
mockMvc
.
perform
(
get
(
"/"
).
accept
(
MediaType
.
ALL
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
content
().
string
(
"index template"
));
});
}
@Test
public
void
prefersAStaticResourceToATemplate
()
{
this
.
contextRunner
.
withUserConfiguration
(
StaticResourceConfiguration
.
class
,
TemplateConfiguration
.
class
).
run
((
context
)
->
{
MockMvc
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
context
).
build
();
mockMvc
.
perform
(
get
(
"/"
).
accept
(
MediaType
.
ALL
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
forwardedUrl
(
"index.html"
));
});
}
@Configuration
static
class
HandlerMappingConfiguration
{
@Bean
public
WelcomePageHandlerMapping
handlerMapping
(
ApplicationContext
applicationContext
,
ObjectProvider
<
TemplateAvailabilityProviders
>
templateAvailabilityProviders
,
ObjectProvider
<
Resource
>
staticIndexPage
,
@Value
(
"${static-path-pattern:/**}"
)
String
staticPathPattern
)
{
return
new
WelcomePageHandlerMapping
(
templateAvailabilityProviders
.
getIfAvailable
(
()
->
new
TemplateAvailabilityProviders
(
applicationContext
)),
applicationContext
,
Optional
.
ofNullable
(
staticIndexPage
.
getIfAvailable
()),
staticPathPattern
);
}
}
...
...
@@ -134,4 +184,43 @@ public class WelcomePageHandlerMappingTests {
}
@Configuration
static
class
TemplateConfiguration
{
@Bean
public
TemplateAvailabilityProviders
templateAvailabilityProviders
()
{
return
new
TestTemplateAvailabilityProviders
((
view
,
environment
,
classLoader
,
resourceLoader
)
->
view
.
equals
(
"index"
));
}
@Bean
public
ViewResolver
viewResolver
()
{
return
(
name
,
locale
)
->
{
if
(
name
.
startsWith
(
"forward:"
))
{
return
new
InternalResourceView
(
name
.
substring
(
"forward:"
.
length
()));
}
return
new
AbstractView
()
{
@Override
protected
void
renderMergedOutputModel
(
Map
<
String
,
Object
>
model
,
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
Exception
{
response
.
getWriter
().
print
(
name
+
" template"
);
}
};
};
}
}
private
static
class
TestTemplateAvailabilityProviders
extends
TemplateAvailabilityProviders
{
TestTemplateAvailabilityProviders
(
TemplateAvailabilityProvider
provider
)
{
super
(
Collections
.
singletonList
(
provider
));
}
}
}
spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
2b1d1cd3
...
...
@@ -1952,9 +1952,7 @@ By default, resources are mapped on `+/**+`, but you can tune that with the
You can also customize the static resource locations by using the
`spring.resources.static-locations` property (replacing the default values with a list of
directory locations). The root Servlet context path `"/"` is automatically added as a
location as well. If you do this, the default welcome page detection switches to your
custom locations. So, if there is an `index.html` in any of your locations on startup, it
is the home page of the application.
location as well.
In addition to the '`standard`' static resource locations mentioned earlier, a special
case is made for http://www.webjars.org/[Webjars content]. Any resources with a path in
...
...
@@ -2022,6 +2020,13 @@ post] and in Spring Framework's
{spring-reference}web.html#mvc-config-static-resources[reference documentation].
====
[[boot-features-spring-mvc-welcome-page]]
==== Welcome Page
Spring Boot support both static and templated welcome pages. It first looks for an
`index.html` file in the configured static content locations. If one is not found, it
then looks for an `index` template. If either is found it is automatically used as the
welcome page of the application.
[[boot-features-spring-mvc-favicon]]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment