1.新建Spring Boot项目

添加spring-boot-starter-thymeleaf依赖

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

2.添加静态文件

根据spring boot默认规则,脚本和样式默认都放在src/main/resources/static下,我这里引入的metro-bootstrap-master(一个开源的模板)

结构如图所示:

3.演示html页面

根据spring boot默认规则,页面放置在src/main/resources/templates下,也可以在application.properties自定义路径。

 ########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh spring.thymeleaf.cache=false

具体html代码如下:

<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html;charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link th:href="@{css/metro-bootstrap.min.css}" rel="stylesheet"/>
<link th:href="@{css/font-awesome.min.css}" rel="stylesheet"/>
<link th:href="@{css/docs.css}" rel="stylesheet"/>
</head>
<body>
<div class="grid">
<div class="row col-md-12">
<div class="tile tile-clouds col-md-3 col-xs-12" >
<h1 th:text="${s[0]}"></h1>
</div>
<div class="tile tile-emerald col-md-3 col-xs-12" >
<h1 th:text="${s[1]}"></h1>
</div>
<div class="tile tile-turquoise col-md-3 col-xs-12" >
<h1 th:text="${s[2]}"></h1>
</div>
<div class="tile tile-amethyst col-md-3 col-xs-12" >
<h1 th:text="${s[3]}"></h1>
</div>
</div>
</div>
</body>
<script th:src="@{js/jQuery/jquery.min.js}"></script>
<script th:src="@{js/bootstrap/bootstrap.min.js}"></script>
<script th:src="@{js/metro-docs.js}"></script>
<script th:inline="javascript">
var s = [[${s}]];
console.log(s);
</script>
</html>

4.视图和数据

新建一个controller,新建一个数组返回数据,代码如下:

package com.haq.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/")
public class SampleController { @RequestMapping("/index")
public String index(Model model){
String[] s = {"我是1","我是2","我是3","我是4"};
model.addAttribute("s",s);
return "index";
} }

5.运行

启动spring boot,返回http://127.0.0.1:8080/index,效果如图所示:

后记:欢迎大家交流学习,有问题还望多多指教,O(∩_∩)O谢谢。

Spring Boot 学习笔记--整合Thymeleaf的更多相关文章

  1. Spring Boot 学习笔记--整合Redis

    1.新建Spring Boot项目 添加spring-boot-starter-data-redis依赖 <dependency> <groupId>org.springfra ...

  2. Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档

    1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...

  3. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  4. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  5. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  6. Spring Boot WebFlux-04——WebFlux 整合 Thymeleaf

    第04课:WebFlux 整合 Thymeleaf 上一篇介绍的是用 MongoDB 来实现 WebFlux 对数据源的操作,那么有了数据需要渲染到前台给用户展示,这就是本文关心的 View 层,Vi ...

  7. Spring Boot 2.0 整合Thymeleaf 模板引擎

    本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...

  8. spring boot整合jsp的那些坑(spring boot 学习笔记之三)

    Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency>            <groupId>or ...

  9. Spring Boot学习笔记:整合Shiro

    Spring Boot如何和Shiro进行整合: 先自定义一个Realm继承AuthorizingRealm,并实现其中的两个方法,分别对应认证doGetAuthenticationInfo和授权do ...

随机推荐

  1. 项目架构开发:数据访问层之Cache

    数据访问层简单介绍 数据访问层,提供整个项目的数据访问与持久化功能.在分层系统中所有有关数据访问.检索.持久化的任务,最终都将在这一层完成. 来看一个比较经典的数据访问层结构图 大概可以看出如下信息 ...

  2. nodejs中的异步流程序控制nsync

    异步编程是指由于异步I/O等因素,无法同步获得执行结果时, 在回调函数中进行下一步操作的代码编写风格,常见的如setTimeout函数.ajax请求等等 http://cnodejs.org/topi ...

  3. Swift应用案例 1.无限轮播

      从今天开始,我学习的重点开始转向Swift,并且会分享一些自己学习的心得体会,今天给大家带来的的是无限轮播.广告页的无限轮播是非常常见的一个功能,大多数APP都有,大多数程序员也都实现过,今天我们 ...

  4. 20170305Meetup Git、heroku drop db

    Rails中,给link_to添加上action和class样式,内部文本 <%= link_to product_path(product),{ :controller => 'cont ...

  5. KoaHub平台基于Node.js开发的Koa EJS渲染插件代码信息详情

    koa-ejs ejs render middleware for koa koa-ejs Koa ejs view render middleware. support all feature of ...

  6. swift -- 类中的方法

    一. 引用类型  类 在类中定义方法 class Person { //属性 var name : String = "" //方法 //实例方法 : 在类里面创建一个方法 fun ...

  7. ajax实现分页和分页查询

    之前有写过ajax的加载页面,是非常简单的,而且不需要重新刷新页面,写起来也是非常的方便,今天写的分页是不用封装page.class.php的, 是单纯的js和ajax写出来的  首先为了页面的整齐与 ...

  8. 使用CEF(CEFGLUE)作为您的客户端UI(一)

    背景: 本人是一名C#开发者,而作为C#开发者,做客户端应用中最头痛的一件事就是没有一个好的UI解决方案.WinFrom嘛,效率虽然还不错,但是做一些特殊的效果,完全应付不来,比如透明控件.比FPS太 ...

  9. JS Math.round()方法原理

    请先测试代码: <!doctype html> <html lang="en"> <head> <meta charset="U ...

  10. JavaScript 方法调用模式和函数调用模式

    这两天在读<JavaScript语言精粹>关于第4章函数调用的几种模式琢磨了半天. 这里就说一下方法调用模式跟函数调用模式. 方法调用模式: 当一个函数被保存为对象的一个属性时,我们称它为 ...