SpringBoot对静态资源的映射规则
- https://www.webjars.org/
所有/webjars/** 都回去classpath"/META-INF/resources/webjars/找资源
webjars:以jar包的方式引入静态资源
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
访问链接:
http://localhost:8080/webjars/jquery/3.0.0/jquery.js
- “/**” 访问当前项目的任何资源(静态资源的文件夹)
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public
“/”:当前项目的根路径
localhost:8080/hello.html
会自动在上面的路径下找对应的静态资源。
- 首页 index.html
- 配置图标
**/favicon.ico 都是在静态资源文件下查找
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="icon" type="image/x-icon" href="/resources/favicon.ico">
</head>
<body>
<h1>hello world</h1>
</body>
</html>
具体源码:WebMvcAutoConfiguration
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
自定义静态文件夹,不推荐,因为原来的会作废。
spring.resources.static-location=classpath:/hello,classpath:/world
1.引入thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
目前我使用的springboot使用了3.0.11 thymeleaf 最新版。
如果后续需要更新版本,可以在pom.xmk文件中配置信息
更新新的版本号,覆盖Springboot原有的版本号:
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>
2.语法使用
文档路径
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.pdf
需要把html页面放到classpath:/templates, thymeleaf就能自动渲染。
1.导入thymeleaf的名称空间
<html xmlns:th="http://www.thymeleaf.org">
2.使用语法: