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

什么是Thymeleaf?

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

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

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

 一、配置maven,在pom.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配置

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

 .properties配置

spring.thymeleaf.prefix=classpath:/templates/

三、新建html页面

 四、controller层

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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

五:访问静态资源

在配置文件当中配置 .yml

spring:
    mvc:
      static-path-pattern: /static/**

引用格式

<link rel="stylesheet" href="../static/css/mystyle.css"/>

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

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

<link rel="shortcut icon" href="../resources/favicon.ico" th:href="@{/static/favicon.ico}"/>

相关推荐

  1. Flutter 利用路由监听页面展示与否

    2024-04-13 17:08:04       58 阅读
  2. springboot针对thymeleaf使用总结

    2024-04-13 17:08:04       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 17:08:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 17:08:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 17:08:04       87 阅读
  4. Python语言-面向对象

    2024-04-13 17:08:04       96 阅读

热门阅读

  1. 数据结构和算法

    2024-04-13 17:08:04       35 阅读
  2. SpringBoot 优雅实现超大文件上传,通用方案

    2024-04-13 17:08:04       30 阅读
  3. uniapp自定义头部导航

    2024-04-13 17:08:04       34 阅读
  4. STM32 中断时间

    2024-04-13 17:08:04       41 阅读
  5. Linux高级IO——多路转接之poll

    2024-04-13 17:08:04       38 阅读
  6. Spring Boot核心注解大全:从入门到精通(三)

    2024-04-13 17:08:04       34 阅读
  7. vue中如何使用render函数

    2024-04-13 17:08:04       38 阅读
  8. 【FAQ】HarmonyOS SDK 闭源开放能力 —Push Kit(2)

    2024-04-13 17:08:04       35 阅读
  9. 理解vuecli和nginx启动Vue项目区别

    2024-04-13 17:08:04       33 阅读