SpringBoot Thymeleaf使用

一、springboot项目默认文件位置

1.文件目录

  • /src/java/resources :Maven的资源文件目录
  • /src/java/resources/static
  • /src/java/resources/public
  • /src/java/resources/templates
  • /src/java/resources/META-INF/resources

2.springboot静态首页的支持

默认情况下,springboot回到上面文件目录路径中找index.html(注意:controller注解不能使用@RestController,要用@Controller,因为@RestController =@Controller+@ResponeBody,会直接返回josn数据)

  • classpath:/META-INF/resources/index.html  
    classpath:/resources/index.html  
    classpath:/static/index.html  
    calsspath:/public/index.html  
    
  • 如果想将index.html放到别的路径下,例如/src/java/resources/templates目录。

    1. 在controller中指定跳转的**页面名称**index

      @RequestMapping("/")  
       public String index(){  
           return "index";  
       }  
    2. 配置文件中修改映射时视图的前后缀(前缀prefix是”classpath:/templates/”,后缀suffix是”.html” )

      spring.thymeleaf.prefix: classpath:/templates/  
      spring.thymeleaf.suffix: .html 
    3. 这样springboot就可以找到/src/java/resources/templates/index.html

二、thymeleaf 3+ 整合到springboot

  1. 由于springboot默认整合的thymeleaf 是2.0.0.RELEASE版本(我使用的springboot是当前稳定版2.0.0.RELEASE)

     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
     <version>2.0.0.RELEASE</version>
     <name>Spring Boot Thymeleaf Starter</name>
     <description>Starter for building MVC web applications using Thymeleaf views</description>
     <url>https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-thymeleaf</url>

  2. 添加thymeleaf 3 依赖,并改版本

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!-- 默认使用Thymeleaf 2.0.0.RELEASE,这里需要使用Thymeleaf 3版本-->
        <!-- 低版本的 Thymeleaf 不能解析 HTML5 格式的 HTML 代码,而是把 HTML 当成 XML 解析 -->
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
    </properties>
    
    <dependencies>
        <!-- 基础组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 测试组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 热部署组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <!-- thymeleaf模板引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork><!-- fork:如果没有该配置,这个devtools不会起作用,即应用不会restart -->
                </configuration>
            </plugin>
        </plugins>
    </build>
  3. spring-boot-starter-web依赖删不删除看清情况

    • 如果thymeleaf中没有web模块,就不需要删除wen

      这里写图片描述

    • 如果thymeleaf中自带了web,就可以删除。

      这里写图片描述

三、使用实例

1.在html页面中引入thymeleaf命名空间

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>testThymeleaf</h1>
</body>
</html>

2.测试http://localhost:8080/

这里写图片描述

四、关于Thymeleaf缓存禁用问题

  • 我使用devtools热部署
  • 就不需要禁掉模板引擎的缓存了(spring.thymeleaf.cache: false )
  • 使用devTool热部署感觉六的飞起
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值