1.模板 引擎简介 :
模板引擎将页面和数据进行分离,简化开发过程,主流模板引擎:Thymeleaf、 FreeMarker、Velocity、Groovy、Mustache 、JSP 、Beetl
Thymeleaf: 优点,主要集中在:模板即原型,前后端分离。缺点:模板必须符合xml规范,速度偏慢。适用于个人独立开发
小结:工作中FreeMarker用的较多,前瞻学习Beetl。现在前端框架例如Bootstrap、Vue较为流行。
2.Thymeleaf依赖引入
(1)在pom.xml 配置文件中添加以下代码引入thymeleaf:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
(2)在html中引入thymeleaf命名空间,如下所示:
<html xmlns:th="http://www.thymeleaf.org" >
3.Thymeleaf 语法
1.常用语法简单介绍
- #{} 主要读取常量,例如读取配置文件中的 数据
- ${} 主要读取变量,程序中创建的,灵活改变的量
- @{} 主要和路径有关系,比如在th:href="@{****}"中的使用
2.更多语法详请参考官方文档
4.Thymeleaf的使用:
1.前端
<tr th:each="book : ${books}">
<td th:text="${book.id}"></td>
<td th:text="${book.name}"></td>
<td th:text="${book.author}"></td>
<td th:text="${book.price}"></td>
<td>
<a th:href="@{'/book/predit/'+${book.id}}" ><button class="btn btn-sm btn-primary">编辑</button></a>
<a th:href="@{'/book/del/'+${book.id}}" ><button class="btn btn-sm btn-danger" >删除</button></a>
</td>
</tr>
2.后端
@Controller
public class bookController {
@Autowired
private BookDao bookDao;
RendDao rendDao;
@RequestMapping("/book")
public String queryAll(Model model){
List<Book> books =bookDao.selectAll();
model.addAttribute("books" ,books);
return "booklist";
}
booklist.html 在templates下,我只需在返回中输入"booklist" Thymeleaf会自动渲染出 booklist.html 。
标签云
ajax AOP Bootstrap cdn Chevereto CSS Docker Editormd GC Hexo IDEA IPA JavaScript jsDeliver JS樱花特效 JVM Linux markdown Maven MyBatis MyBatis-plus MySQL Pictures Sakura SEO shadowrocket Spring Boot Spring Cloud Spring Cloud Alibaba SpringMVC SSR Thymeleaf V2ray Vue Web WebSocket Wechat Social WordPress Yoast SEO 代理 分页 图床 小幸运 苹果iOS国外账号 苹果IOS账号
Comments | NOTHING