Thymeleaf

将后台从数据库中查询到的数据填到静态模板中的占位符的位置

将Thymeleaf依赖添加到pom.xml文件中

<dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

另有一说:引入Thymeleaf的依赖为:

      <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.1.0.RC1</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

在application.propertiers进行配置:

#排除静态文件夹
spring.devtools.restart.exclude=static/**,public/**

#关闭 Thymeleaf 缓存开发过程中无须重启
spring.thymeleaf.cache = false

#设置thymeleaf页面的编码
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5

#设置thymeleaf页面的后缀为.html
spring.thymeleaf.suffix=.html

#设置thymeleaf页面的存储路径
spring.thymeleaf.prefix=classpath:/templates/

在Controller包中建立一个UserController类,在里面写一个addUser()方法:

@Controller 
public class UserController
{
   @GetMapping("/addUser")
   public String  addUser(Model  model)
   {
       model.addAttribute("title","I miss CC very much");
       return   "user/addUser";
     //自动跳转到resources/templates/user/addUser.html,也就是说会自动将字符串拼接到resources/templates后面
   }
}

在resources/templates下新建一个user目录,然后再新建一个addUser.html文件:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
标题是:
<p th:text="${title}">hello</p>
</body>
</html>

当在浏览器中输入/addUser时,会调用这里的addUser方法,会跳转到resources文件夹下面的templates目录下的user目录下的addUser.html页面(同时设置数据title为i miss CC very much)

在浏览器中访问localhost:8080/addUser

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值