zoukankan      html  css  js  c++  java
  • springboot整合Thymeleaf

    1. Thymeleaf简介

    Thymeleaf是一种用于Web和独立环境的现代服务器端的Java模板引擎。

    Thymeleaf的主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易地协作。Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。

    Thymeleaf使用Spring框架的模块,与许多常见的工具集成在一起,并且可以插入自己的功能,是现代HTML5 JVM Web开发的理想选择,尽管Thymeleaf还有更多其它的功能。

    Thymeleaf建立在自然模板的概念之上,以不影响模板作为设计原型的方式将其逻辑注入到模板文件中。 这改善了设计沟通,弥合了前端设计和开发人员之间的理解偏差。

    2. 在pom.xml中加入Thymeleaf依赖

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    3. 创建web应用程序

    使用Thymeleaf模板在Spring Boot中创建Web应用程序。必须按照以下步骤使用Thymeleaf在Spring Boot中创建Web应用程序。

    使用以下代码创建@Controller注解类文件以将Request URI重定向到HTML文件 

    package com.sicq.publicity.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    
    @Controller
    @RequestMapping(value = "home")
    public class HomeController {
    
        @RequestMapping(value = "/index")
        public String index() {
            return "index";
        }
    }
    

    4.新增html页面

    在上面的示例中,请求URI是/index,被重定向到index.html文件。 请注意,index.html 文件应放在templates目录下,所有JS和CSS文件应放在static目录下。 

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h4>Thymeleaf Spring Boot web应用程序示例</h4>
    </body>
    </html>
    

    5.查看结果

    启动应用程序,打开浏览器访问URL => http://localhost:8080/home/index.

  • 相关阅读:
    eclipse中的debug模式的使用
    Hibernate快速入门
    鸟哥的 Linux 私房菜Shell Scripts篇(四)
    Vim命令图解及快捷键讲解
    SpringBoot文档
    鸟哥的 Linux 私房菜Shell Scripts篇(三)
    鸟哥的 Linux 私房菜Shell Scripts篇(二)
    (二)IDEA使用,快捷键
    (一)IDEA使用,基础配置
    idea-git同步服务器代码后撤销操作
  • 原文地址:https://www.cnblogs.com/xxrl-c/p/11281109.html
Copyright © 2011-2022 走看看