Spring Boot 2.6.4 (5) - Web Develop (Part A)

"Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-in Event, click to view the details of the event ."

1. Web Development in Spring Boot

Use IDEA to create a project Spring Boot project spring-boot-restful, select basic web dependencies and Thymeleaf template engine dependencies.

image.pngThe automatic configuration of Spring Boot has completed a lot of configuration, and we only need a small amount of configuration to complete the creation of a Web project.

Create a new controller package under the com.lilith package, add HelloController, add a hello method, and use annotations to configure access paths.

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello, Spring Boot!";
    }
}
复制代码

Start the main program and enter http://localhost:8080/hello in the browser image.png

Through these steps, a web project is created. Compared with Spring MVC, there is almost no configuration. The configuration is all completed by a large number of XxxAutoConfiguration automatic configuration classes in Spring Boot, and all the configurations that can be customized are in the XxxProperties configuration class.

Spring Boot's mapping rules for static resources

Access to public static resources

The Web automatic configuration class of Spring Boot is org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; the addResourceHandlers method in this class is used to define resource access, and the access path of webjars is added to this method; that is to say All /webjars/** can go to classpath:/META-INF/resources/webjars/ to find resources in the classpath

image.png webjars就是将前端资源以jar包的方式进行访问;前端资源的jar包可以在 webjars官网 获取。

在pom文件中添加jQuery的jar包

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>
复制代码

引入的jquery静态资源就在webjars目录下 image.png

重新启动应用,在浏览器访问静态资源 http://localhost:8080/webjars/jquery/3.3.1/jquery.js image.png

私有静态资源访问方式

对于私有静态静态资源的访问方式定义在addResourceHandlers方法中的第338行

image.png

this.mvcProperties.getStaticPathPattern()
复制代码

上面这行代码代表的路径是 /** image.png 接着的lambda表达式代码代表的路径如下图所示 image.png

addResourceHandlers方法中定义的第二种访问方式是访问当前项目的任何资源既/**,如果没有方法处理,就自动去以下这些目录位置去查找资源

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

这些文件夹也称为静态资源文件夹。

尝试访问这几个路径,分别新建对应的文件夹,resources目录就是classpath:/。

classpath:/META-INF/resources/

新建META-INF/resources文件夹,添加一个index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/META-INF/resources目录</h1>
</body>
</html>
复制代码

重新启动应用,在浏览器中访问 localhost:8080/index.html image.png 成功访问到META-INF/resources 目录下的index.html文件

classpath:/resources/

在原始的resources目录下再新建一个resources目录,注意不是META-INF下的resources目录,放入index1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/resources目录</h1>
</body>
</html>
复制代码

重新启动应用,浏览器访问localhost:8080/index1.html image.png

classpath:/static/

在classpath:/ 类路径下的static文件夹新建index2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/static目录</h1>
</body>
</html>
复制代码

重新启动应用,浏览器访问localhost:8080/index2.html image.png

classpath:/public/

在classpath:/下新建public文件夹,添加index3.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<h1>classpath:/public/</h1>
</body>
</html>
复制代码

重新启动应用,浏览器访问localhost:8080/index3.html

image.png 也可以成功访问

Spring Boot 欢迎页

Spring Boot 中的WelcomePageHandlerMapping类中定义了欢迎页的配置

image.png 也就是说 / 路径会转发到 静态资源文件夹下的index.html页面上

在浏览器输入localhos:8080 image.png 根据页面显示默认找到了在META-INF/resources目录下的index.html文件作为欢迎页

自定义静态资源路径

WebProperties下的Resources类属性中有一个setStaticLocations方法,该方法可以自定义静态文件夹的路径 image.png

在properties配置文件中配置自定义的静态资源路径

# 覆盖以前所有的静态资源路径
spring.web.resources.static-locations=classpath:/lilith,
复制代码

启动程序,再次访问index1.html

image.png

找不到index1.html页面,说明默认的静态文件夹已经不再是静态文件夹了,被自定义的设置覆盖了。需要注意的是欢迎页可以正常访问

在classpath:/路径下新增lilith文件夹,新增index3.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>classpath:/lilith</h1>
    <h2>这是自定义的静态资源配置</h2>
</body>
</html>
复制代码

重启启动程序,访问index3.html

image.png 自定义的静态资源文件夹生效,可以正常访问。

ICON 配置

在Spring Boot项目的issues中提出,如果提供默认的Favicon可能会导致网站信息泄露。如果用户不进行自定义的Favicon的设置,而Spring Boot项目会提供默认的上图图标,那么势必会导致泄露网站的开发框架。

因此,在Spring Boot2.2.x中,将默认的favicon.ico移除,同时也不再提供上述application.properties中的属性配置。

Click to view Spring Boot icon issue

Guess you like

Origin juejin.im/post/7079816379813068813