动态资源: 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....