thymeleaf 排除list中的某个数据_26、模板引擎thymeleaf

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

客户端渲染:

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

服务端渲染:

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

a7ce4a0de22682e97befe929048aa952.png

相较与其他的模板引擎,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

b6b65e38f1237f5c749803bc58001ea4.png

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值