Spring Boot 学习笔记--整合Thymeleaf
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的更多相关文章
- Spring Boot 学习笔记--整合Redis
1.新建Spring Boot项目 添加spring-boot-starter-data-redis依赖 <dependency> <groupId>org.springfra ...
- Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档
1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- Spring Boot WebFlux-04——WebFlux 整合 Thymeleaf
第04课:WebFlux 整合 Thymeleaf 上一篇介绍的是用 MongoDB 来实现 WebFlux 对数据源的操作,那么有了数据需要渲染到前台给用户展示,这就是本文关心的 View 层,Vi ...
- Spring Boot 2.0 整合Thymeleaf 模板引擎
本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...
- spring boot整合jsp的那些坑(spring boot 学习笔记之三)
Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency> <groupId>or ...
- Spring Boot学习笔记:整合Shiro
Spring Boot如何和Shiro进行整合: 先自定义一个Realm继承AuthorizingRealm,并实现其中的两个方法,分别对应认证doGetAuthenticationInfo和授权do ...
随机推荐
- c#之循环效率
很多人在保存数据时候对于使用数组.集合等?然后遍历数据的时候是for.froeach? 下面我就写一个小例子进行测试看看,话不多说,直接用数据说话. 1.构建数据分别是数组.集合构建,数据类型分别是值 ...
- JS传值和传引用
之前看<javascript高级程序设计>时就遇到过一个问题,就是函数传参的问题,书上说是按值传递,而非按引用传递.很多人都会认为js里面有两种传值方式,一种是复制,另一种是数组.对象等按 ...
- ERP和MES破冰之路 [普实MES升级中国“智”造]
题记:早在2007年,普实就提出AIO一体化产品概念,全力打造ERP的资源闭环,并取得了良好的市场效应.如今,在制造业的智能生产需求下,生产制造执行系统(MES)成功推向市场. MES是什么? 制造执 ...
- 最近新版本的pangolin出现了点问题,我把可用的旧版本上传到了github
经测试该版本编译没有问题. 下载地址:https://github.com/zzx2GH/Pangolin.git
- Cocos2d-x shader学习1: 灰度shader
灰度shader 最近在学习shader,就把cocos2d-x 3.x版本中的很简单也很常用的灰度shader拿出来学习一下. #ifdef GL_ES precision mediump floa ...
- Spark_总结五
Spark_总结五 1.Storm 和 SparkStreaming区别 Storm 纯实时的流式处理,来一条数据就立即进行处理 SparkStreaming ...
- QtQuick 中的 qml 与 Qt 的 C++
QtQuick 可以使用内置的 JavaScript 引擎加载相应的 JS 代码,使用起来特别方便. 在 Qt 中使用 C++ 开发底层,QtQuick 用来加载.处理图像,然后使用 Qt 提供的接口 ...
- 使用jQuery监听扫码枪输入并禁止手动输入的实现方法
@(知识点总结)[jquery|扫码抢] 基于jQuery的扫码枪监听.如果只是想实现监听获取条码扫码信息,可以直接拿来使用,如果有更多的条码判断处理逻辑需要自己扩展. 一.功能需求 使用扫码枪扫描条 ...
- macaca环境搭建(web 和 android)
一.安装配置JDK 1.1下载JDK地址http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...
- css gray,grayscale,css变灰兼容大部分浏览器
css gray,grayscale,css变灰兼容大部分浏览器 html { filter: url("data:image/svg+xml;utf8,<svg xmlns=\'ht ...