一、静态资源映射规则
ResourceProperties.java
指定了静态文件加载的位置及顺序:
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties implements ResourceLoaderAware {
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
WebMvcAutoConfiguration.java
public class WebMvcAutoConfiguration {
public static final String DEFAULT_PREFIX = "";
public static final String DEFAULT_SUFFIX = "";
//添加静态文件映射路径
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
}
}
}
所有 /webjars/** ,都会从** classpath:/META-INF/resources/webjars/** 找资源;

二、webjars使用
官网:webjars.org
作用:通过使用导入jar的形式来引用静态文件: 例如:
<!--引入jquery资源-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>1.11.1</version>
</dependency>

三、thymeleaf模板引擎
常用的模板引擎:jsp,freemarker,thymeleaf等。springboot 官方推荐使用thymeleaf

3.1引入thymeleaf模板
<!--导包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--更改默认版本使用thymeleaf 3.x -->
<properties>
<!--设定版本-->
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局组件,thymeleaf 3.x 需要layout 2.x以上版本-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
ThymeleafProperties.java 设置模板的默认值
@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";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML5";
模板默认的加载位置:classpath:/templates/ *.html文件
3.2使用thymeleaf
a.引用命名空间
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
b.语法规则
<!--emp:集合的元素,itState:状态,需要遍历的值-->
<tr th:each="emp,itState: ${empList}">
<td><input type="checkbox" name="checkbox_name"> <span th:text="${itState.index+1}"></span></td>
<td th:text="${emp.empno}">1,001</td>
<td th:text="${emp.ename}">Lorem</td>
<td th:text="${emp.job}">ipsum</td>
<!--设置日期格式-->
<td th:text="${#dates.format(emp.hiredate,'yyyy-MM-dd')}">dolor</td>
<td th:text="${emp.sal}">sit</td>
<td th:text="${emp.dname}">sit</td>
<td><a class="btn btn-sm btn-primary" href="#" th:href="@{'/emp/'+${emp.empno}}"><span
class="glyphicon glyphicon-edit"></span>编辑</a>
<!--重新定义一个属性:myhref,设置新的url路径-->
<a class="btn btn-sm btn-danger btn-del" th:attr="myhref=@{'/emp/'+${emp.empno}}" href="javascript:void(0)" th:value="${emp.empno}">
<span class="glyphicon glyphicon-trash"></span>删除</a></td>
</tr>
具体语法参照:thymeleaf帮助文档
网友评论