相对html+js的传统设计,现在很多网站都采用div&css+标签化+模块化的设计。模板引擎根据一定的语义,将数据填充到模板中,产生最终的HTML页面。模板引擎主要分两种:客户端引擎和服务端引擎。

客户端渲染:

模板和数据分别传送到客户端,在客户端由JavaScript模板引擎渲染出最终的HTML视图。将模板渲染放置在客户端做,可以降低服务端的压力,并且如果前端内容分别来自多个后台系统,而这些后台的架构各不相同(Java、.NET、Ruby等),则服务器端渲染需要采用不同的技术,模板资源无法共享。

服务端渲染:

引擎在服务器端将模板和数据合成,返回最终的html页面,相对于客户端渲染,数据存储更加安全。主要有freemarker、velocity、thymeleaf等。

相较与其他的模板引擎,thymeleaf它有如下三个特点:

(a) 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,同时也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

(b) 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

(c) 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

1、新建项目sc-thymeleaf,对应的pom.xml文件如下

4.0.0spring-cloud sc-thymeleaf 0.0.1-SNAPSHOTjarsc-thymeleafhttp://maven.apache.orgorg.springframework.boot spring-boot-starter-parent 2.0.4.RELEASEorg.springframework.cloud spring-cloud-dependencies Finchley.RELEASEpomimportorg.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf 

2、新建springboot启动类

package sc.thymeleaf;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ThymeleafApplication { public static void main(String[] args) { SpringApplication.run(ThymeleafApplication.class, args); }}

3、新建配置文件application.yml

server: port: 8090spring: application: name: sc-thymeleaf thymeleaf: cache: false

说明:thymeleaf所有的配置项可以参考类

org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties

常用配置说明:

# THYMELEAF (ThymeleafAutoConfiguration)

#开启模板缓存(默认值:true)

spring.thymeleaf.cache=true

#Check that the template exists before rendering it.

spring.thymeleaf.check-template=true

#检查模板位置是否正确(默认值:true)

spring.thymeleaf.check-template-location=true

#Content-Type的值(默认值:text/html)

spring.thymeleaf.content-type=text/html

#开启MVC Thymeleaf视图解析(默认值:true)

spring.thymeleaf.enabled=true

#模板编码

spring.thymeleaf.encoding=UTF-8

#要被排除在解析之外的视图名称列表,用逗号分隔

spring.thymeleaf.excluded-view-names=

#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)

spring.thymeleaf.mode=HTML5

#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)

spring.thymeleaf.prefix=classpath:/templates/

#在构建URL时添加到视图名称后的后缀(默认值:.html)

spring.thymeleaf.suffix=.html

#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。

spring.thymeleaf.template-resolver-order=

#可解析的视图名称列表,用逗号分隔

spring.thymeleaf.view-names=

其实完全可以使用不用配置,但是Spring Boot官方文档建议在开发时将缓存关闭,默认为true

4、新建Controller

package sc.thymeleaf.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 sc.thymeleaf.model.User;@Controllerpublic class UserController { @RequestMapping("/user/list") public String userList2(Model model) throws Exception { model.addAttribute("hello

js取thymeleaf值_26、模板引擎thymeleaf相关推荐

  1. 模板引擎 Thymeleaf 语法

    模板引擎 Thymeleaf 1. Thymeleaf 简介 Thymeleaf[taɪm lif],百里香叶,是一个流行的模板引擎,该模板引擎采用 Java 语言开发.Java 中常见的模板引擎有 ...

  2. SpringBoot-web开发(三): 模板引擎Thymeleaf

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) 目录 1. 引入 ...

  3. JavaWeb~模板引擎Thymeleaf总结

    文章目录 模板引擎是什么 有哪些常见的模板引擎 Thymeleaf使用流程 常用标签 链接表达式@{...} 变量表达式${...} 选择变量表达*{...} 消息表达式#{...}(了解) 回顾:几 ...

  4. Spring Boot (四)模板引擎Thymeleaf集成

    一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...

  5. Spring Boot 最佳实践(四)模板引擎Thymeleaf集成

    ## 一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供X ...

  6. 六十四、SpringBoot中的模板引擎Thymeleaf

    @Author:Runsen 来源:尚硅谷 下面建议读者学习尚硅谷的B站的SpringBoot视频,我是学雷丰阳视频入门的. 具体链接如下:B站尚硅谷SpringBoot教程 文章目录 使用Sprin ...

  7. thymeleaf 获取yml中的值_Thymeleaf模板引擎学习

    开发传统Java WEB项目时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用JSP页面进行页面渲染了.从而Thymeleaf提供了一个用于整合Spring MVC的可选模 ...

  8. Spring boot(五)模板引擎 Thymeleaf

    Thymeleaf 是一款用于渲染 XML/XHTML/HTML 5 内容的模板引擎.类似 JSP.Velocity.FreeMaker 等,它也可以轻易的与 Spring MVC 等 Web 框架进 ...

  9. SpringBoot中模板引擎thymeleaf

    首先我们用SpringBoot创建一个支持thymeleaf的web项目 添加web支持 添加thymeleaf模板引擎 创建好该项目之后,在templates目录下创建一个普通的html文件,这个时 ...

最新文章

  1. Hibernate hbm2ddl.auto配置的可能值是什么,它们做了什么
  2. 天翼云从业认证(4.7)天翼云安全基础实践
  3. C++/CLI中的资源清理(Destructor,Finalizer)
  4. mysql 排序字段是否需要建索引_MySQL索引详解(优缺点,何时需要/不需要创建索引,索引及sql语句的优化)...
  5. 怎么做fastreport使用离线数据源
  6. 基于点特征的各位姿求解算法对比(pose-estimation-compared)
  7. 管理站点复制 【Windows Server 2019】活动目录(Acitve Directory)——在同一区域安装多台域控制器
  8. JUnit编写单元测试代码注意点小结
  9. V4L2视频采集与H264编码4—X264编码H264视频
  10. mysql 5.7.26卸载_MySQL 5.7.26安装与卸载
  11. php递归实例,php递归经典案例
  12. VMware 8超级详细的安装说明
  13. Android耗电统计算法
  14. 2019互联网BATJ等大厂中秋礼盒大PK
  15. 外国小伙发现乌云是黑色的……
  16. 证监会叫停VR等行业跨界定增,福兮祸兮?
  17. android判断字符串是否包含下划线,android 富文本SpannableString去掉下划线
  18. 第八十二章 Caché 函数大全 $ZCSC 函数
  19. 收藏这几个软件,让你轻松从视频里提取音频
  20. NBUT1225-NEW RDSP MODE I

热门文章

  1. 平安夜海报PNG免扣素材来了,全都在这|搜图114
  2. 最新 UI 色彩渐变素材模板|设计师好帮手
  3. 广东科技学院计算机原理组成,201120122操作系统原理期中试卷edited广东科技学院付博士(4页)-原创力文档...
  4. docker 容器内部获取自身id_crontab入门二:定时启动docker容器并启动容器内部脚本...
  5. Javascript实现页面跳转传值示例Demo
  6. 学习计划(11.5)
  7. 什么是I/O地址,I/O端口和I/O端口地址?
  8. linux mtk unique id,1.2.16.1. Nand Flash的Unique ID
  9. python查找最长的字符串_Python简单实现查找一个字符串中最长不重复子串的方法...
  10. Django文件上传***