SpringBoot_04静态资源映射规则和模板引擎Thymeleaf
静态资源映射规则和模板引擎Thymeleaf
本⼈是个新⼿,写下博客⽤于⾃我复习、⾃我总结。
如有错误之处,请各位⼤佬指出。
学习资料来源于:尚硅⾕
使⽤SpringBoot
1)、创建SpringBoot应⽤,选中我们需要的模块;
2)、SpringBoot默认将这些场景配置好了,只需要在配置⽂件中指定少量配置就可以运⾏起来;
3)、⾃⼰编写业务代码;
简例
先像前⼏篇⽂章⼀样,创建⼀个SpringBoot应⽤,这次选中web模块。
⽣成好之后,最基本的web的快速开发,只要像下⾯这样,建⼀个controller(实现了⼀个简单的helloworld),就可以完成,像之前⼀样。
HelloController.java
package com.ller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return"Hello World";
}
}
然后在⽹页上,输⼊localhost:8080/hello,即可查看到Hello World。
那么对于CSS⼀类的⽂件,应该放在什么位置上,其实是有规定的,这就有关于SpringBoot对静态资源的映射规则。SpringBoot对静态资源的映射规则
先来分析⼀段 WebMvcAutoConfiguration.class 中的⼀段代码:
public void addResourceHandlers(ResourceHandlerRegistry registry){
if(!sourceProperties.isAddMappings()){
logger.debug("Default resource handling disabled");
}else{
Duration cachePeriod =Cache().getPeriod();
CacheControl cacheControl =Cache().getCachecontrol().toHttpCacheControl();
if(!registry.hasMappingForPattern("/webjars/**")){
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"cla sspath:/META-INF/resources/webjars/"}).Seconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern =StaticPathPattern();
if(!registry.hasMappingForPattern(staticPathPattern)){
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(StaticLocations())).Seconds(cachePeriod)).setCacheControl(cac heControl));
}
}
}
//配置欢迎页
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConve rsionService, ResourceUrlProvider mvcResourceUrlProvider){
WelcomePageHandlerMapping welcomePageHandlerMapping =new WelcomePageHandlerMapping(n
ew TemplateAvailabilityProviders(application Context), WelcomePage(),StaticPathPattern());
welcomePageHandlerMapping.Interceptors(mvcConversionService, mvcResourceUrlProvider));
return welcomePageHandlerMapping;
}
//配置图标
@configuration
适合新手的spring boot@ConditionalOnProperty(value ="spring.abled", matchIfMissing =true)
public static class FaviconConfiguraion {
public final ResourceProperties resourceProperties;
public FaviconConfiguraion(ResourceProperties resourceProperties){
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping(){
SimpleUrlHandlerMapping mapping =new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE +1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler(){
ResourceHttpRequestHandler requestHandler =new ResourceHttpRequestHandler();
requestHandler
.FaviconLocations());
return requestHandler;
}
}
根据代码:
1)、所有 /webjars/** ,都去classpath:/META-INF/resources/webjars/ 下资源;
⽐如,我想调⽤jquery的3.3.1版本,
就在l,dependencies标签中加⼊:(这段引⼊在官⽹中可以到)
<!-- 引⼊jquery-webjar -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
之后就可以到:
也就是说,可以在⽹页中输⼊ localhost:8080/webjars/jquery/3.3.1/jquery.js ,它就可以到这个⽂件。那么在访问的时候,只需要写webjars下⾯资源的名称即可。
2)、"/**" 访问当前项⽬的任何资源,(静态资源的⽂件夹)
“classpath:/META-INF/resources/”
“classpath:/resources/”
“classpath:/static/”
“classpath:/public/”
“/”:当前项⽬的根路径
也就是说,只要⽂件⽆法处理,就会在这些⽂件夹中寻。
在⽹页中输⼊localhost:8080/abc,只要⽆响应,就会去这些⽂件夹中寻abc。
3)、欢迎页;静态资源⽂件夹下的所有index.html⽂件;
输⼊localhost:8080/ 就会⾃动去寻静态资源⽂件夹下的index页⾯
⽐如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>⾸页</h1>
</body>
</html>
4)、配置图标;所有的 **/favicon.ico 都是在静态资源⽂件夹下寻。
这样打开页⾯,就会发现出现图标。(是上⾯的那个⼩图标)
5)、当然也可以⼿动修改路径,但是没必要。
在application.properties中:
模板引擎Thymeleaf
SpringBoot的项⽬是以jar包的形式,⽽且是嵌⼊式的tomcat,所以它默认是不⽀持JSP的。那么使⽤静态资源的⽅式,就会带来很多的⿇烦。因此,SpringBoot就提供了模板引擎。就⽐如JSP、Velocity、Freemarker、Thymeleaf等都是模板引擎。模板引擎,就是我们先写⼀个静态模板,但是其中有⼀些值是动态的,这些值⼜会从⼀些指定数据中获得,那么将这些数据和模板交给模板引擎,由模板引擎来帮我们组装、解析、填充到指定的位置,最后将这个数据写出去。
SpringBoot推荐的是Thymeleaf,它语法更简单,功能更强⼤。
引⼊thymeleaf:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
切换thymeleaf版本时需要注意,每⼀个thymeleaf都有对应的layout,具体对应版本,去github上直接搜索thymeleaf 和 thymeleaf-layout-dialect 或者去官⽹,可以查。但是版本问题会经常出现(我尝试了好久),以下为我⾃⼰能⽤的版本号。
<properties>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<!-- 布局功能的⽀持程序 thymeleaf3主程序适配layout2以上版本-->
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
Thymeleaf使⽤和语法
⾸先,从ThymeleafProperties.class中看到以下代码:
@ConfigurationProperties(
prefix ="spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
public static final String DEFAULT_PREFIX ="classpath:/templates/";
public static final String DEFAULT_SUFFIX =".html";
,,,,
}
也就是说,只要我们把HTML页⾯放在classpath:/templates/ 下,thymeleaf就能⾃动渲染。package com.ller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return"Hello World";
}
@RequestMapping("/success")
public String success(){
return"success";
}
}
success.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
</body>
</html>
使⽤:
1、导⼊thymeleaf的名称空间
<html lang="en"th="">
2、使⽤thymeleaf语法(简例)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。