SpringBoot学习篇3[静态资源、Thymeleaf模板引擎]

开始之前

在开始之前,先创建项目spring-boot-web
在这里插入图片描述
添加Spring Web和Thymeleaf依赖
在这里插入图片描述
勾选那两项是为了在Maven中引入以下两个依赖:
在这里插入图片描述
创建好后的项目目录如下(框起来的部分可以删除):
在这里插入图片描述

一 静态内容

1.1 静态资源的查找路径

默认情况下,Spring Boot从类路径中名为/static、/public、/resources或/META-INF/resources的目录下查找静态内容。
在resources下创建这三个目录:static、public、resources,并在static目录下创建hello.html文件,文件写入以下内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>静态资源文件</title>
</head>
<body>
    <h2>这是static目录下的静态资源文件</h2>
</body>
</html>

启动/重启项目,访问一下http://localhost:8080/hello.html
在这里插入图片描述
将hello.html文件移动到public或resources目录后,重启项目,再访问一下:
在这里插入图片描述
在这里插入图片描述

1.2 修改资源文件映射路径

默认情况下,资源映射到/**(即在浏览器中通过http://localhost:8080/xxx即可访问到xxx资源文件),也可以使用spring.mvc.static-path-pattern属性对其进行调整。例如,将所有资源重新映射到/resources/**可以通过以下方式实现:

spring.mvc.static-path-pattern=/resources/**

重启项目,再访问一下,发现已经访问不到了:
在这里插入图片描述
在url上加上/resources/后再试一下‘
在这里插入图片描述

1.2 欢迎页面

Spring Boot支持静态和模板欢迎页面。它首先在静态资源文件的查找目录中查找index.html文件,如果未找到,则寻找index模板文件。如果找到任何一个,它将自动作为web应用的欢迎界面(即在浏览器中通过http://localhost:8080直接访问到的页面)。
创建index.html文件,并将其放入任一静态资源文件路径下,写入以下内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>这是首页</h1>
</body>
</html>

重启项目,在浏览器访问一下(注意:如果修改了资源文件映射路径的话,将访问不到
在这里插入图片描述

1.3 自定义图标

Spring Boot支持自定义图标,在这里提供两种方式:
方法一:
Spring Boot将在静态资源文件目录查找favicon.ico文件,如果找到了,将以此作为web应用的图标(需要清除浏览器缓存才能看得到)。
在这里插入图片描述
方法二:
1.配置spring.mvc.favicon.enabled属性为false
2.在index.html中添加以下代码

<link rel="icon" type="image/x-icon" href="/images/favicon.ico">

方法二有点投机取巧的意思。在访问首页时,浏览器缓存了web应用的图标,而后只要不清除缓存,浏览器显示的就是首页设定的图标。

二 模板引擎

2.1 Spring Boot支持的模板引擎

Spring MVC支持提供动态HTML内容。Spring MVC支持各种模板技术,包括Thymeleaf,FreeMarker和JSP。
SpringBoot对以下模板引擎做了自动配置的支持:

  • FreeMarker
  • Groovy
  • Thymeleaf
  • Mustache

SpringBoot官方不推荐使用JSP。将JSP与嵌入式Servlet一起使用时,将存在一些局限,具体可参考官方文档

在默认情况下,使用这些模板引擎之一时,Spring Boot将从resources/templates目录下提取模板引擎。

在这里插入图片描述

2.2 使用Thymeleaf模板引擎

2.2.1 引入Thymeleaf

  • 确保pom.xml文件中有添加依赖

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  • 在使用Thymeleaf的html页面上声明名称空间

    <html xmlns:th="http://www.thymeleaf.org">
    
  • Thymeleaf不支持像访问静态资源那样直接被访问。为了看到效果,编写Controller调用我们的Thymeleaf测试文件。

    package com.yky.springboot.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class ThymeleafTestController
    {
         
        @GetMapping(value = "/thymeleaf")
        public String callMyThymeleaf()
        {
         
            return "ThymeleafTest";
        }
    }
    
    

ThymeleafTest放到了templates/ThymeleafTest.html路径下。

2.3 Thymeleaf标准表达式语法

2.3.1 message表达式

格式: #{…}

message表达式用于从资源文件中读取内容,并显示在页面上。message表达式的主要用途是用于显示国际化信息,这部分知识将会在本篇文章后面讲解。

<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>标题</title>
</head>
<body>
    <p th:text="#{home.welcome}">
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中使用Thymeleaf模板引擎访问静态HTML的过程如下: 1. 在Spring Boot工程中创建一个静态HTML文件,例如index.html。 2. 在application.properties文件中添加以下配置: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 这个配置告诉Thymeleaf模板引擎,它应该在classpath:/templates/目录下查找HTML模板文件,后缀为.html。 3. 创建一个Controller,用于处理请求并将数据传递给模板: ```java @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, world!"); return "index"; } } ``` 这个Controller处理根路径的GET请求,并将一个名为message的属性添加到Model中。然后,它返回index作为视图的名称。 4. 在index.html中使用Thymeleaf模板引擎来呈现message属性: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 这个HTML文件使用Thymeleaf的th:text属性来呈现message属性。 5. 启动Spring Boot应用程序,并访问http://localhost:8080/,应该看到Hello, world!。 注意:在上述步骤中,我们将HTML文件放在了classpath:/templates/目录下,这是因为Thymeleaf默认会在这个目录下查找模板文件。如果你想将HTML文件放在其他地方,可以在application.properties文件中通过配置spring.thymeleaf.prefix属性来指定模板文件的路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值