基于SpringBoot开发一套完整的项目(三)准备工作

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 
  • 21
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 45
    评论
评论 45
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值