0
点赞
收藏
分享

微信扫一扫

SpringBoot WEB篇


先使用SpringBoot初始化器初始化一个SpringBoot工程

SpringBoot WEB篇_jar

初始化后的项目结构是这样的

SpringBoot WEB篇_jar_02


WEB么肯定就会有静态界面

SpringBoot对静态资源的映射规则
这里的映射规则核心是这个,这个在WebMvcAutoConfiguration类中

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
//spring.resources这个配置中可以设置和静态资源相关的参数,如缓存时间等

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}

所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源;

webjars:以jar包的方式引入静态资源,说白了就是html中引入的js以Maven的方式导入使用

这是人家优秀的官网​​wenjars​​ 我们导入jquery试试

SpringBoot WEB篇_html_03


SpringBoot WEB篇_jar_04


localhost:8080/webjars/jquery/3.3.1/jquery.js

SpringBoot WEB篇_spring boot_05


静态界面Html中要使用的时候只需要写webjars下面资源的名称;

访问"/**"的时候默认会在去这些路径中找资源

  1. “classpath:/META-INF/resources/”
  2. “classpath:/resources/”
  3. “classpath:/static/”
  4. “classpath:/public/”

    欢迎页设置,静态资源所以有的index.html
    网站图标设置,静态资源中所有的favicon.ico

//这个可以设置静态资源的路径,设置完后会覆盖原有的路径
spring.resources.static-locations=classpath:/hello/,classpath:/tao/

模板引擎(Html的数据填充框架)

JSP、Velocity、Freemarker、Thymeleaf

SpringBoot WEB篇_spring_06


这里只介绍Thymeleaf,而且SpringBoot推荐使用这种高级的模板引擎;

使用简单pom中导入

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

这段代码是设定自动渲染的路径和文件

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

public static final String DEFAULT_PREFIX = "classpath:/templates/";

public static final String DEFAULT_SUFFIX = ".html";
//只需要我们把HTML界面放到classpath:/templates/下面thymeleaf就能自动帮我们渲染

我们在controller中写一个接口

@Controller
//这里不要用@RestController否则返回json不会去thymeleaf里面找html文件
public class Test {
@RequestMapping("/test")
public String test(){
return "test";//这个会直接在路径中拼成/templates/test.html
}
}

HTML中使用Thymeleaf模板语法提示

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">//导入名称空间Thymeleaf的语法提示
<head>
<meta charset="UTF-8">
<title>test</title>
</head>

这里的Thymeleaf详细语法不过多解释了,因为现在开发中大多是前后端分离的架构


举报

相关推荐

0 条评论