2.SpringBoot利用Thymeleaf实现页面的展示

什么是Thymeleaf?

Thymeleaf是一个现代服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本。

Thymeleaf的主要目标是提供一种优雅且高度可维护的模板创建方式。为实现这一目标,它以自然模板的概念为基础,将其逻辑注入模板文件,其方式不会影响模板被用作设计原型。这改善了设计沟通,缩小了设计和开发团队之间的差距。

Thymeleaf也从一开始就设计了Web标准 - 特别是HTML5 - 允许您创建完全验证的模板,如果您需要的话。

一、配置maven,在pom.xml当中配置

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>

二、配置Thymeleaf

.yml配置

XML 复制代码
spring:
   thymeleaf:
       cache: false # 关闭页面缓存
       encoding: UTF-8 # 模板编码
       prefix: classpath:/templates/  # 页面映射路径
       suffix: .html # 试图后的后缀
       mode: HTML5 # 模板模式

.properties配置

XML 复制代码
spring.thymeleaf.prefix=classpath:/templates/

三、新建html页面

四、controller层

java 复制代码
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }
}

五:访问静态资源

在配置文件当中配置 .yml

java 复制代码
spring:
    mvc:
      static-path-pattern: /static/**

引用格式

XML 复制代码
<link rel="stylesheet" href="../static/css/mystyle.css"/>

我们在进行正常访问的时候会报一下的错误,这是因为我们需要给网页标题前添加一个小图标favicon.ico。

我们需要加上这一句话在页面上。

XML 复制代码
<link rel="shortcut icon" href="../resources/favicon.ico" th:href="@{/static/favicon.ico}"/>
相关推荐
风象南5 小时前
很多人说,AI 让技术平权了,小白也能乱杀老师傅 ?
人工智能·后端
雨中飘荡的记忆6 小时前
ElasticJob分布式调度从入门到实战
java·后端
Se7en2587 小时前
推理平台全景
后端
大漠_w3cpluscom7 小时前
你学不会 CSS,不是笨,是方向错了
后端
cipher10 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
毅航11 小时前
自然语言处理发展史:从规则、统计到深度学习
人工智能·后端
JxWang0511 小时前
Task04:字符串
后端
树獭叔叔12 小时前
10-让模型更小更聪明,学而不忘:知识蒸馏与持续学习
后端·aigc·openai
JxWang0512 小时前
Task02:链表
后端