“Offer comes, ask friends to take it! I am participating in the 2022 Spring Recruit Punch card campaign. Click here for more details.”

Web development in Spring Boot

Spring Boot project Spring-boot-restful, select basic Web dependencies and Thymeleaf template engine dependencies.

Spring Boot already has a lot of automatic configuration done, we need only a small amount of configuration to complete a Web project creation.

Create a controller package under the com.lilith package, add HelloController, add the Hello method, and configure the access path using annotations.

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(a){
        return "Hello, Spring Boot!"; }}Copy the code

Start the main program and type in your browserhttp://localhost:8080/hello

With these steps, a Web project is created. Compared to Spring MVC, almost no configuration is done. The configuration is all done by the large XxxAutoConfiguration class in Spring Boot, and the configurable configuration is all in the XxxProperties configuration class.

Spring Boot mapping rules for static resources

Public static resource access

Spring Boot Web automatically configure class is org. Springframework. Boot. The autoconfigure. Web. Servlet. WebMvcAutoConfiguration; The addResourceHandlers method in this class defines resource access, adding the WebJars access path; That is, all /webjars/** can be found in classpath:/ meta-INF /resources/webjars/

Webjars is about accessing front-end resources as JARS. Jar packages for front-end resources are available inWebjars websiteTo obtain.

Add the jQuery JAR to the POM file

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>
Copy the code

The jquery static resources introduced are in the WebJars directory

Restart the application and access static resources in the browserhttp://localhost:8080/webjars/jquery/3.3.1/jquery.js

Private static resource access

Access to private static resources is defined at line 338 in the addResourceHandlers method

this.mvcProperties.getStaticPathPattern()
Copy the code

The path represented by the above line is /**The path represented by the following lambda expression code is shown below

The second access method defined in the addResourceHandlers method is to access any resource of the current project either /** or, if there is no method to handle it, automatically go to the following directory locations to find the resource

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

These folders are also called static resource folders.

Try to access each of these paths by creating a new folder, the resources directory is classpath:/.

classpath:/META-INF/resources/

Create a meta-INF /resources folder and add an index. HTML

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>The classpath: / meta-inf/resources directory</h1>
</body>
</html>
Copy the code

Restart the application, in the browser to access localhost: 8080 / index. HTMLThe index.html file in the meta-INF /resources directory was successfully accessed

classpath:/resources/

Create a new resources directory under the original resources directory, not the resources directory under meta-INF, and put in index1.html

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>The classpath: / resources directory</h1>
</body>
</html>
Copy the code

Restart the application, the browser access localhost: 8080 / index1. HTML

classpath:/static/

Create index2.html in the static folder of classpath:/

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>The classpath: / static directory</h1>
</body>
</html>
Copy the code

Restart the application, the browser access localhost: 8080 / index2. HTML

classpath:/public/

Create a new public folder under classpath:/ and add index3.html

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <h1>classpath:/public/</h1> </body> </html>Copy the code

Restart the application, the browser access localhost: 8080 / and index3. HTML

It can also be accessed successfully

Spring Boot Welcome page

In the Spring the Boot WelcomePageHandlerMapping class defines the configuration of the welcome page

That is, the/path is forwarded to the index.html page in the static resources folder

Type localHOs :8080 in your browserBy default, the welcome page is found in the index.html file in the meta-INF/Resources directory

Custom static resource paths

The Resources class property under WebProperties has a setStaticLocations method that allows you to customize the path to a static folder

Configure a custom static resource path in the Properties configuration file

Overwrite all previous static resource paths
spring.web.resources.static-locations=classpath:/lilith,
Copy the code

Launch the program and access index1.html again

The index1.html page is not found, indicating that the default static folder is no longer a static folder and has been overridden by custom Settings. Note that the welcome page can be accessed normally

Add index3.html to lilith folder in classpath:/

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/lilith</h1>
    <h2>This is a custom static resource configuration</h2>
</body>
</html>
Copy the code

Restart the launcher and access index3.html

The customized static resource folder takes effect and can be accessed normally.

ICON to configure

As mentioned in the Issues of the Spring Boot project, providing the default Favicon may result in site information leakage. If the user does not set up a custom Favicon, and the Spring Boot project provides the default icon above, it will give away the site’s development framework.

Therefore, in Spring Boot2.2.x, the default favicon.ico is removed, and the above property configuration in application.properties is no longer provided.

Click to view Spring Boot Icon Issue