table of Contents
1. Homepage
Also in the
WebMvcAutoConfugure
class automatically configure adaptation classesWebMvcAutoConfigurationAdapter
There are three methods, regarding homepage
1. Analyze the source code
The first is the welcome page processing mapping class WelcomePageHandler
, which @Bean
is injected into the bean through annotations, and it obtains the resource path in two ways:
- Custom resource path
this.mvcProperties.getStaticPathPattern()
- Call to
gerWelcomPage
get the resource path
Among the getWelcomPage
methods, there is a getStaticLocations
method that returns locations
the four static resource paths mentioned above
String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
"classpath:/META-INF/resources/":就是上述的webjars
"classpath:/resources/":reources目录下的resources目录,不存在自己新建
"classpath:/static/":resources目录下的static目录
"classpath:/public/":resources目录下的public目录,不存在自己新建
The getWelcomePage
method is also called in the getIndexHtml
method to configure the home page, and the location is locations
under the aboveindex.html
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
Summary : We index.html
can directly access and load by placing the homepage file in any one of the four directories of static resources
2. Direct access to the homepage test
We can put the homepage index.html
in any of the following directories
"classpath:/META-INF/resources/":就是上述的webjars
"classpath:/resources/":reources目录下的resources目录,不存在自己新建
"classpath:/static/":resources目录下的static目录
"classpath:/public/":resources目录下的public目录,不存在自己新建
Here , create a new resource
directory under the public
directory to index.html
indicate the home page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
Then run the main program test, visit localhost:8080
, successfully loaded to the home page
3. Jump to the homepage by request
Normally, we need to jump to the homepage by request, so we need to write a controller to jump to the view, so where is this page?
In the resource
next, except public
, resource
, staic
three static directory, and a template
directory, so we can put our home in the directory, it is loaded into the home by request
-
By default, the springboot project does not allow direct access
templates
to the files under it, and is protected. -
If you want to access the files under templates, you need the support of the template engine, it is recommended to use
thymeleaf
, importthymeleaf
dependencies here
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Then, we move the above index.html
into the templates
package
and write anotherIndexController
package com.zsr.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
Restart the main program to access the test, access /index
, successfully display the home page
Two, icon settings
- In earlier versions, Spring Boot
Favicon
provided default support. You only need to place the.ico
icon file in any of the four static resource directories, and you need toapplication.properties
turn off the default icon in the following configuration
spring.mvc.favicon.enabled=false #关闭
-
However, it is proposed in the issues of the Spring Boot project that if the default favicon is provided, it may lead to the disclosure of website information. If the user does not customize the Favicon settings, and the Spring Boot project will provide the default icon above, it will inevitably lead to the disclosure of the development framework of the website.
-
Therefore, in Spring Boot 2.2.x, the default favicon.ico is removed, and the property configuration in the above application.properties is no longer provided
After this, we need
index.html
to configure in the home page file
- The
.ico
picture on the resources folder under the static file folder
- Introduce images in the home page index.html
<link rel="icon" href="/favicon.ico">
-
Just visit the test
Note : The test may be unsuccessful, it is recommended to clear the browser cache and then refresh