SpringBoot-web development (2): Home page and icon customization (source code analysis)



1. Homepage

Also in the WebMvcAutoConfugureclass automatically configure adaptation classes WebMvcAutoConfigurationAdapterThere are three methods, regarding homepage

image-20200922110236212

1. Analyze the source code

image-20200922110753710
The first is the welcome page processing mapping class WelcomePageHandler, which @Beanis injected into the bean through annotations, and it obtains the resource path in two ways:

  • Custom resource paththis.mvcProperties.getStaticPathPattern()
  • Call to gerWelcomPageget the resource path

Among the getWelcomPagemethods, there is a getStaticLocationsmethod that returns locationsthe 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目录,不存在自己新建

image-20200921200824407
The getWelcomePagemethod is also called in the getIndexHtmlmethod to configure the home page, and the location is locationsunder the aboveindex.html

private Resource getIndexHtml(String location) {
    
    
   return this.resourceLoader.getResource(location + "index.html");
}

Summary : We index.htmlcan 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.htmlin 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 resourcedirectory under the publicdirectory to index.htmlindicate the home page
image-20200922120403520

<!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

image-20200922120447750

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 resourcenext, except public, resource, staicthree static directory, and a templatedirectory, 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 templatesto 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, import thymeleafdependencies here

<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Then, we move the above index.htmlinto the templatespackage
image-20200922121044553
and write anotherIndexController
image-20200922121451329

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
image-20200922164346883




Two, icon settings

  • In earlier versions, Spring Boot Faviconprovided default support. You only need to place the .icoicon file in any of the four static resource directories, and you need to application.propertiesturn 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

    image-20200922172115803

After this, we need index.htmlto configure in the home page file

  1. The .icopicture on the resources folder under the static file folder
  2. Introduce images in the home page index.html
<link rel="icon" href="/favicon.ico">
  1. Just visit the test
    image-20200922234440062

    Note : The test may be unsuccessful, it is recommended to clear the browser cache and then refresh

Guess you like

Origin blog.csdn.net/qq_45173404/article/details/108743904