springboot学习八:与thymeleaf整合

动态资源:  JSP(spring boot默认不支持)

   推荐:模板引擎 thymeleaf

   网页= 模板+数据

 

一、引入thymeleaf:到官网查询 thymeleaf的依赖(Maven)

        <dependency>
			<groupId>org.thymeleaf</groupId>
			<artifactId>thymeleaf-spring5</artifactId>
		</dependency>
		<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-java8time</artifactId>
		</dependency>

 

二、使用thymeleaf:代码在哪里写?

ThymeleafAutoCongifutation(ThymeleafAutoConfiguration) -->@EnableConfigurationProperties(ThymeleafProperties.class)-->ThymeleafProperties.class

通过ThymeleafProperties源码得知:

使用thymeleaf只需要将 文件放入目录:"classpath:/templates/";  文件的后缀: ".html";

 

注意:在以前传统的web项目中:静态资源修改后  是不需要重启的;但是在spring boot项目中,修改后 需要重启。

注意@ResponseBody注解的作用

 

三、在html文件中添加thymeleaf

1.html头部标签添加<html xmlns:th="http://www.thymeleaf.org">

2.<p th:text="${welcome}">welcome to thymeleaf....</p>以上,先从${welcome}中取值,如果有 则直接显示;如果没有,则在显示welcome to thymeleaf....

th就是替换原有html的值:th:html属性名=值 ;

<p id="pid" class="pclass"  th:id="${welcome}" th:class="${welcome}"  th:text="${welcome}">welcome to thymeleaf....</p>

一定要重启

th:xx  (参见第10章 Attrubite Pre....)

th:text   获取文本值    显示 将hello 渲染为h1后的效果

th:utext  获取文本值(h1不转义) 显示<h1>hello</h1>

 

符号

th:text="${welcome}" ,除了$以外  其他符号? 查看第四章  Standard Express....

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值