小D课堂 - 零基础入门SpringBoot2.X到实战_第7节 SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf_31、SpringBoot2整合模板引擎thymeleaf实战

笔记

4、SpringBoot2.x整合模板引擎thymeleaf实战
    讲解:SpringBoot2.x整合模板引擎thymeleaf实战

    官网地址:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html
    1、thymeleaf相关maven依赖
            <dependency>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>

    2、thymeleaf基础配置

        #开发时关闭缓存,不然没法看到实时页面
        spring.thymeleaf.cache=false
        spring.thymeleaf.mode=HTML5
        #前缀
        spring.thymeleaf.prefix=classpath:/templates/
        #编码
        spring.thymeleaf.encoding=UTF-8
        #类型
        spring.thymeleaf.content-type=text/html
        #名称的后缀
        spring.thymeleaf.suffix=.html

    3、建立文件夹
        1)src/main/resources/templates/tl/
        2)建立一个index.html

    4、简单测试代码编写和访问
        注意:$表达式只能写在th标签内部
        快速入门:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html

开始 


配置文件



配置的跟路径直接指名了是templates下的tl文件夹作为根路径




controller


启动应用





把templates文件夹加入到springboot的默认加载路径里面去。在application.properties这里spring.resources.static-locations里面配置

所以需要把templates也加入到配置中。这样templates文件就会作为springboot默认加载会扫描加载的静态资源文件




取数据,跳转到静态页




th:text:模板语法替换了测试内容

直接访问静态页

快速入门:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html




 

猜你喜欢

转载自www.cnblogs.com/wangjunwei/p/11421765.html
今日推荐