ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot教程(2):整合Thymeleaf(上)

2020-02-05 10:55:38  阅读:201  来源: 互联网

标签:templates index 教程 SpringBoot boot springframework Thymeleaf org


一、为什么使用Thymeleaf

Thymeleaf是SpringBoot推荐使用的前端模板引擎。

Thymeleaf官方给出的定义是:

  Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.
  The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates. To achieve this, it builds on the concept of Natural Templates to inject its logic into template files in a way that doesn’t affect the template from being used as a design prototype. This improves communication of design and bridges the gap between design and development teams.
  Thymeleaf has also been designed from the beginning with Web Standards in mind – especially HTML5 – allowing you to create fully validating templates if that is a need for you.

二、Spring Boot 整合 Thymeleaf

1、在pom.xml文件引入thymeleaf

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

        <!-- SpringBoot整合thymeleaf模板 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- SpringBoot整合thymeleaf模板 -->

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  

2、添加index.html

 Thymeleaf有一个默认配置,配置信息放在了 org.springframework.boot.autoconfigure.thymeleaf 下的 ThymeleafProperties类中,具体如下:

 

所以Thymeleaf会默认在 "classpath:/templates/" 寻找html的文件,所以我们首先在resources下添加文件夹templates,再在templates文件夹下新建index.html文件,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
----------------------------------------------------------------------<br/>
这是Index.html页面<br/>
----------------------------------------------------------------------<br/>
</body>
</html>

 

3、添加Java类:IndexController

  截图如下:

 

   代码如下:

package com.demo.web.controller;

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

@Controller
public class IndexController {

    @RequestMapping(value = {"","/","/index"}) //多个路由匹配
    //@RequestMapping("/index") //单个路由匹配
    public String Index(){
        return "index";
    }
}

三、运行

打开浏览器,输入网址:http://localhost:8080/

 

 四、总结

  这应该算是最简单的Spring Boot 集成 Thymeleaf的方案!下一章将介绍一些Thymeleaf的基础用法!

标签:templates,index,教程,SpringBoot,boot,springframework,Thymeleaf,org
来源: https://www.cnblogs.com/yansg/p/12260063.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有