1. springboot集成Thymeleaf模版
对于动态HTML 内容的展示,模板引擎必不可少,为什么选择Thymeleaf,由于Thymeleaf “原型即页面”的特点,非常适用于快速开发,符合Spr ing Boot 开箱即用的原则。所谓“原型即页面,简单一句话:
界面的设计与实现相分离,这就是Thymeleaf 广为流行的原因
2.Thymeleaf 标准表达式语法
以th 属性开头,例如 < span th : text= ”…”>
变量表达式: ${……}
选择表达式:*{……}
消息表达式:#{……}
链接表达到: @{……}
分段表达式: ~{……}
等等,详细的可以自行了解
3. springboot 集成 thymeleaf
首先添加依赖,点进去可以看到版本的。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
可以看到spring-boot-starter-thymeleaf主要依赖了以下三个,默认是 3.0.9.RELEASE,也就是最新版本,如果要修改版本的话,再添加依赖的时候再指定一个Version属性就行,这里不再赘述
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.2.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.9.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> <version>3.0.1.RELEASE</version> <scope>compile</scope> </dependency>
4 .Thymeleaf 实战
首先在application.properties中添加
创建接口UserRepository和实现类UserRepositoryImpl
public interface UserRepository { User saveOrUpdateUser(User user); //新增或者修改用户 void deleteUsere(Long id); //删除用户 User getUserById(Long id); //根据用户id获取用户 List<User> userList (); //获取所有用户的列表 }
@Repository//用于标识UserRepositoryimpl 类是一个可注入的bean 。 public class UserRepositoryImpl implements UserRepository { //用来生成一个递增的id ,作为用户的唯一编号。 private static AtomicLong counterId = new AtomicLong(); //模拟数据的存储, private final ConcurrentMap<Long ,User> userConcurrentMap