1.SpringBoot对静态资源的映射规则
webjars导包
自定义js,css,img等
欢迎页
默认访问静态资源下的index.html
图标
默认访问静态资源下的favicon.ico
2.模板引擎thymeleaf
2.1 SpringBoot引入thmeleaf
pom.xml两处配置
<properties>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
记个坑:thymeleaf无法跳转
2.2 thymeleaf 语法规则
2.2.1 映射规则:
将html页面放于classpath:template/下,thymeleaf就可自动渲染。
package zkrun.top.springbootwebrestfulcrud.bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ControllerTest
{
@RequestMapping("/jump")
public String test()
{
return "success";
}
}
2.2.2 导入命名空间(语法提示)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h1>jump to thymeleaf!</h1>
</body>
</html>
2.2.3 表达式与标签的简单使用
Controller
package zkrun.top.springbootwebrestfulcrud.bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
import java.util.Map;
@Controller
public class ControllerTest
{
@RequestMapping("/jump")
public String test(Map<String,Object>maps)
{
maps.put("name","<h1>galring</h1>");
maps.put("user", Arrays.asList("user1","user2","user3"));
return "success";
}
}
success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h1>jump to thymeleaf!</h1>
<hr>
<div th:text="${name}"></div>
<div th:utext="${name}"></div>
<hr>
<h1 th:each="user:${user}">[[${user}]]</h1>
<h1>
<span th:text="${user}" th:each="user:${user}"></span>
</h1>
</body>
</html>