zoukankan      html  css  js  c++  java
  • Spring Boot Thymeleaf

    Thymeleaf是一个基于Java的库,用于创建Web应用程序。 它为在Web应用程序中提供XHTML/HTML5提供了很好的支持。 在本章中将详细了解和学习Thymeleaf。

    Thymeleaf模板

    Thymeleaf将文件转换为格式良好的XML文件。它包含6种类型的模板,如下所示 -

    • XML
    • 有效的XML
    • XHTML
    • 有效的XHTML
    • HTML5
    • 旧版HTML5

    除旧版HTML5之外的所有模板都指的是格式正确的有效XML文件。 旧版HTML5允许我们在网页中呈现HTML5标记,包括非封闭标记。

    Web应用程序

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

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

    //原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html
    package com.yiibai.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class WebController {
       @RequestMapping(value = "/index")
       public String index() {
          return "index";
       }
    }//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html

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

    使用以下代码并在单独的文件夹css 中创建一个CSS文件,并将该文件命名为styles.css -

    //原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html
    h4 {
       color: red;
    }//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html

    index.html 文件的代码如下 -

    <!DOCTYPE html>
    <html>
       <head>
          <meta charset = "UTF-8" />
          <link href = "css/styles.css" rel = "stylesheet"/>
          <title>Spring Boot应用程序(Yiibai.com)</title>
       </head>
       <body>
          <h4>Thymeleaf Spring Boot web应用程序示例</h4>
          <p>
              Power by Yiibai.com
          </p>
       </body>
    </html>//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html

    现在,需要在构建配置文件中添加Spring Boot Starter Thymeleaf依赖项。

    Maven用户可以将以下依赖项添加到pom.xml 文件中 -

    //原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html

    Maven构建文件 - pom.xml 的代码如下 -

    <?xml version = "1.0" encoding = "UTF-8"?>
    <project xmlns = "http://maven.apache.org/POM/4.0.0" 
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 
       http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.yiibai</groupId>
       <artifactId>demo</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>
       <name>demo</name>
       <description>Demo project for Spring Boot</description>
    
       <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>1.5.8.RELEASE</version>
          <relativePath />
       </parent>
    
       <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
          <java.version>1.8</java.version>
       </properties>
    
       <dependencies>
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
    
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
          </dependency>
    
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-thymeleaf</artifactId>
          </dependency>
       </dependencies>
    
       <build>
          <plugins>
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
          </plugins>
       </build>
    
    </project>//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_thymeleaf.html
  • 相关阅读:
    ajax封装
    完美运动框架
    表单上传input=file
    面向对象入门
    浅谈javaScript内存
    关于使用iframe的父子页面进行简单的相互传值
    浅谈原生JavaScript的动画和特效
    rem 原理与简介
    移动 web 适配
    jsonp 简单封装
  • 原文地址:https://www.cnblogs.com/0710whh/p/12397738.html
Copyright © 2011-2022 走看看