thymeleaf 比如freemaker的要高,thymeleaf是一个支持html原型的自然引擎,它在html 标签增加额外的属性来达到模板+数据的展示方式,由于 浏览器解释html时,忽略未定义的标签属性,因此thym eleaf的模板可以静态运行。
由于thymeleaf在内存缓存解析后的模板,解析后的模板 是基于tree的dom节点树,因此thymeleaf适用于一般 的web页面,不适合基于数据的xml。
thymeleaf 的context,即提供数据的地方,基于web 的context,即WebContext相对context增加 param,s ession,application变量,并且自动将request atttribu tes添加到context variable map,可以在模板直接访 问。
在模板处理前,thymeleaf还会增加一个变量execInf o,例:${execInfo.templateName},${execInfo.n ow}等。
数据访问模式: ${...},变量引用模式,例:${myBean.propert y},如果用springDialect,则使用的是spring EL,如 果不用spring,则用的ognl。

1、*{...},选择表达式,一般是th:object之后,直接取ob ject中的属性。当没有选取对象时,其功能等同${...},

2、* {firstName}也等同于${#object.firstName},#obje ct代表当前选择的对象。

3、@{...}链接url的表达式。th:href="@{/xxx/aa.do(I d=${o.id})",会自动进行url­encoding的处 理。

4、@{...}内部可以是需要计算的表达式,比如: th:href=”@{'/details/'+${user.login}(orderI d=${o.id})}"

后续我会补上thymeleaf 指令的是用法…

一、创建maven web project

二、编写测试类

1、新建一个sysuser类

package com.example.entry;

public class SysUser  {
private Long id;
private String name;
private String Phone; public SysUser() {
}
public SysUser(Long id, String name, String phone) {
this.id = id;
this.name = name;
Phone = phone;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
@Override
public String toString() {
return "SysUser [id=" + id + ", name=" + name + ", Phone=" + Phone + "]";
} }

2、编写SysUserController类  :这里需要注意倒入的包

package com.example.controller;

import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView; import com.example.entry.SysUser; @Controller
@RequestMapping("sysUser")
public class SysUserController { @RequestMapping("index")
public String index(Model m) {
List<SysUser> sysUserList = new ArrayList<SysUser>();
SysUser u1 = new SysUser(11L, "AAAAA", "123456");
SysUser u2 = new SysUser(22L, "BBBBB", "123456");
SysUser u3 = new SysUser(33L, "CCCCC", "123456");
sysUserList.add(u1);
sysUserList.add(u2);
sysUserList.add(u3);
m.addAttribute("sysUserList", sysUserList);
m.addAttribute("message", sysUserList.hashCode());
return "SysUser/list";
}
}

3、加入html文件  :这里使用的是bootstrap-3.3.7

<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
<meta content="text/html;charset=utf-8"></meta>
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<script sec="../jquery-2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="../bootstrap-3.3.7/dist/css/bootstrap.min.css"></link>
<script sec="../bootstrap-3.3.7/dist/js/bootstrap.min.js"></script>
<title>用户</title>
</head>
<body>
<div class="container">
<table class="table">
<div th:if="${message!=null}">
<caption th:text="${message}"></caption>
</div>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>aehyok</td>
<td>leo</td>
<td>@aehyok</td>
</tr>
<tr>
<td>lynn</td>
<td>thl</td>
<td>@lynn</td>
</tr>
</tbody>
<tbody th:each="SysUser:${sysUserList}">
<tr>
<td th:text="${SysUser.id}"></td>
<td th:text="${SysUser.name}"></td>
<td th:text="${SysUser.Phone}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

4、配置application.properties支持thymeleaf

spring.thymeleaf.suffix=.html
spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

5、测试访问

6、访问view层

默认响应的view是的路径是templates这个目录

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.ResourceUrlProvider; @Controller
public class HelloController { @Autowired
ResourceUrlProvider resourceUrlProvider; @RequestMapping("index")
public ModelAndView index() {
ModelAndView mv = new ModelAndView("index");
return mv;
}
@RequestMapping("demo")
public ModelAndView demo() {
ModelAndView mv = new ModelAndView("demo");
return mv;
}
@RequestMapping("signin")
public ModelAndView signin() {
ModelAndView mv = new ModelAndView("signin");
return mv;
}
}

十三.spring-boot使用spring-boot-thymeleaf的更多相关文章

  1. Spring boot(三)整合mybaties+thymeleaf实现基础crud

    工程结构: 首先在pom文件中引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...

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

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

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

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

  4. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  5. 在Spring MVC和Spring Boot中使用thymeleaf模板

    Spring MVC: POM: <!-- thymeleaf模板 --> <!-- https://mvnrepository.com/artifact/org.thymeleaf ...

  6. 9、Spring Boot 2.x 集成 Thymeleaf

    1.9 Spring Boot 2.x 集成 Thymeleaf 完整源码: Spring-Boot-Demos 1.9.1 在pom中引入依赖 <dependency> <grou ...

  7. Spring Boot 2.x教程-Thymeleaf 原理是什么

    layout: post title: Spring Boot 2.x教程-Thymeleaf 原理是什么 categories: SpringBoot description: Spring Boo ...

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

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

  9. Spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)

    近期上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 以下是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一  导入框架所需的包,我们用的事maven 进行 ...

  10. 一:Spring Boot、Spring Cloud

    上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...

随机推荐

  1. window.screen.height和window.screen.availHeight和document.body.clientHeight和document.documentElement.clientHeight

    说这几个属性前 我说一下我的设备 我的设备有两个,一个高度为1080的显示器,一个高度为800的电脑 第一种:window.screen.height 这个方法是获取用户电脑屏幕的高度,是不关浏览器或 ...

  2. 弹出框美化 alert样式美化

    引用style.css和ui.js就可以直接用以下接口调用!(文末附完整代码) alert_带标题: mizhu.alert('alert_带标题', '这是alert效果'); alert_带图标: ...

  3. NET应用——你的数据安全有必要升级

    最近又被[现场破解共享单车系统]刷了一脸,不得不开始后怕:如何防止类似的情况发生? 想来想去,始终觉得将程序加密是最简单的做法.但是摩拜.ofo也有加密,为什么仍然被破解?那是因为请求在传输过程中被篡 ...

  4. 动态创建timer

    Private  timer:Ttimer;procedure MyTimerDo(Sender:Tobject);procedure create ;  timer:=TtIMER.Create;  ...

  5. Linux下使进程在后台运行

    怎么样使程序在后台执行 ///////////////////  nohup  ./nn > nn.log  2 > &1  &   //////////// 方法有很多, ...

  6. 转:fortios 5.4后门植入

    提示: 1.经过实验,fortios 5.4 beta4也是可以的. 2.在实验时,选择先下载fortios 5.2(做了快照),再升级5.4,则虚拟机挂载需要选择FortiGate-VM-disk1 ...

  7. 洛谷P1330 封锁阳光大学 [图论,染色]

    题目传送门 封锁阳光大学 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构 ...

  8. ExtJs之组件(window)

    Ext.create('Ext.window.Window',{    title:'',    width:400,    height:300,    constrain:true,//限制窗口不 ...

  9. 删除元素(LintCode)

    删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 返回 4 ...

  10. 解决Linux环境下安装xampp之后外部无法连接MySQL的问题

    在Linux系统下,开发PHP一般都是LAMP环境,对于开发环境来讲,没有必要花太大精力去单独配置LAMP环境,采用xampp一键安装包是一个很好的方式.在Linux系统上安装xampp的过程这里就不 ...