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}"/>
相关推荐
吠品18 小时前
Wine 在 Linux 上运行 Windows 软件完整指南
java·linux·服务器
2601_9537208218 小时前
【计算机毕业设计】基于Vue与Spring Boot的高校兼职信息服务平台设计与实现
spring boot·后端·课程设计
亚历克斯神19 小时前
智能搜索系统的升级复盘——从 Elasticsearch 到混合检索的检索质量提升
java·spring·微服务
再吃一根胡萝卜19 小时前
用 Rust 写一个桌面悬浮图标:为什么它比 Python 更适合 AI 桌面工具?
后端
IT_陈寒20 小时前
Vue的computed属性把我坑惨了,原来我一直用错姿势
前端·人工智能·后端
金銀銅鐵20 小时前
[Java] 一个方法最多可以有多少个入参?
java·jvm
哭哭啼20 小时前
JAVA服务问题诊断
java·开发语言·jvm
evans在进步21 小时前
Spring Boot 核心机制详解:可执行 JAR、CORS、静态资源与配置绑定
spring boot·后端·jar
Sayuanni%321 小时前
SpringBoot 从注解到源码:核心知识点总结
java·spring boot·后端
坚定信念,勇往无前21 小时前
Maven 私有仓库-nexus
java