springboot模板的两种方式
在上一篇的博文中我们以经入门springboot了,在springboot中我们将告别以前我们的写前端的方式jsp。有的只是HTML和ftl页面,分别对应两种模板
Thymeleaf模板
关于Thymeleaf的优点,我只说一条:它就是html页面。
导入pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
//正式环境还是要将缓存开启的
spring.thymeleaf.cache=false
对应的后台代码:
实体类
package com.hu.springboot01.entity;
import lombok.Data;
@Data
public class User {
private Integer uid;
private String userName;
private String password;
public User(Integer uid, String userName, String password) {
this.uid = uid;
this.userName = userName;
this.password = password;
}
public User() {
}
}
controller层代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>list</title>
</head>
<body>
springboot初步使用thymeleaf
<h2>显示文本</h2>
<span th:text="${name}"></span>
<hr>
<h2>显示HTML</h2>
<div th:utext="${msg}"></div>
<hr>
<h2>循环</h2>
<table>
<tr>
<td>用户的ID</td>
<td>用户名</td>
<td>密码</td>
</tr>
<tr th:each="u :${userList}">
<td th:text="${u.uid}"></td>
<td th:text="${u.userName}"></td>
<td th:text="${u.password}"></td>
</tr>
</table>
<hr style="color: crimson">
<h2>综合使用</h2>
<li th:each="u : ${userList}">
<a href="#" th:title="${u.uid }" th:text="${#strings.length(u.userName) < 4 ? u.uid : #strings.substring(u.password, 0, 16)+'...' }">热点新闻1</a>
</li>
<h2>如何在页面定义变量</h2>
<div th:include="role/common/head2"></