Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >首页JavaThymeleaf 模板引擎与Spring Boot

首页JavaThymeleaf 模板引擎与Spring Boot

作者头像
cherishspring
发布于 2019-10-21 09:32:58
发布于 2019-10-21 09:32:58
1.2K00
代码可运行
举报
文章被收录于专栏:Java学习笔记Java学习笔记
运行总次数:0
代码可运行

1,Thymeleaf是什么 ? Thymeleaf 是一个Java类库,是一个xml/xhtml/html5的模板引擎,可以作为MVC的Web应用的View层。 Thymeleaf 还提供额外与Spring MVC集成,可以使用Thymeleaf完全替代JSP。 2,Spring Boot 自动配置 Thymeleaf

Spring Boot 通过org.springframework.boot.autoconfigure.thymeleaf包对Thymeleaf进行自动配置。如下图:

通过ThymeleafProperties来配置Thymeleaf,在application.properties中,以spring.thymeleaf开头来配置,通过查看ThymeleafProperties的主要源码,我们可以看到如何设置属性及默认配置:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

	public static final String DEFAULT_PREFIX = "classpath:/templates/";

	public static final String DEFAULT_SUFFIX = ".html";

	/**
	 * Whether to check that the template exists before rendering it.
	 */
	private boolean checkTemplate = true;

	/**
	 * Whether to check that the templates location exists.
	 */
	private boolean checkTemplateLocation = true;

	/**
	 * Prefix that gets prepended to view names when building a URL.
	 */
	private String prefix = DEFAULT_PREFIX;

	/**
	 * Suffix that gets appended to view names when building a URL.
	 */
	private String suffix = DEFAULT_SUFFIX;

	/**
	 * Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.
	 */
	private String mode = "HTML";

	/**
	 * Template files encoding.
	 */
	private Charset encoding = DEFAULT_ENCODING;

	/**
	 * Whether to enable template caching.
	 */
	private boolean cache = true;

	/**
	 * Order of the template resolver in the chain. By default, the template resolver is
	 * first in the chain. Order start at 1 and should only be set if you have defined
	 * additional "TemplateResolver" beans.
	 */
	private Integer templateResolverOrder;

	/**
	 * Comma-separated list of view names (patterns allowed) that can be resolved.
	 */
	private String[] viewNames;

	/**
	 * Comma-separated list of view names (patterns allowed) that should be excluded from
	 * resolution.
	 */
	private String[] excludedViewNames;

	/**
	 * Enable the SpringEL compiler in SpringEL expressions.
	 */
	private boolean enableSpringElCompiler;

	/**
	 * Whether hidden form inputs acting as markers for checkboxes should be rendered
	 * before the checkbox element itself.
	 */
	private boolean renderHiddenMarkersBeforeCheckboxes = false;

	/**
	 * Whether to enable Thymeleaf view resolution for Web frameworks.
	 */
	private boolean enabled = true;

	private final Servlet servlet = new Servlet();

	private final Reactive reactive = new Reactive();

	public boolean isEnabled() {
		return this.enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public boolean isCheckTemplate() {
		return this.checkTemplate;
	}

	public void setCheckTemplate(boolean checkTemplate) {
		this.checkTemplate = checkTemplate;
	}

	public boolean isCheckTemplateLocation() {
		return this.checkTemplateLocation;
	}

	public void setCheckTemplateLocation(boolean checkTemplateLocation) {
		this.checkTemplateLocation = checkTemplateLocation;
	}

	public String getPrefix() {
		return this.prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getSuffix() {
		return this.suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getMode() {
		return this.mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public Charset getEncoding() {
		return this.encoding;
	}

	public void setEncoding(Charset encoding) {
		this.encoding = encoding;
	}

	public boolean isCache() {
		return this.cache;
	}

	public void setCache(boolean cache) {
		this.cache = cache;
	}

	public Integer getTemplateResolverOrder() {
		return this.templateResolverOrder;
	}

	public void setTemplateResolverOrder(Integer templateResolverOrder) {
		this.templateResolverOrder = templateResolverOrder;
	}

	public String[] getExcludedViewNames() {
		return this.excludedViewNames;
	}

	public void setExcludedViewNames(String[] excludedViewNames) {
		this.excludedViewNames = excludedViewNames;
	}

	public String[] getViewNames() {
		return this.viewNames;
	}

	public void setViewNames(String[] viewNames) {
		this.viewNames = viewNames;
	}

	public boolean isEnableSpringElCompiler() {
		return this.enableSpringElCompiler;
	}

	public void setEnableSpringElCompiler(boolean enableSpringElCompiler) {
		this.enableSpringElCompiler = enableSpringElCompiler;
	}

	public boolean isRenderHiddenMarkersBeforeCheckboxes() {
		return this.renderHiddenMarkersBeforeCheckboxes;
	}

	public void setRenderHiddenMarkersBeforeCheckboxes(boolean renderHiddenMarkersBeforeCheckboxes) {
		this.renderHiddenMarkersBeforeCheckboxes = renderHiddenMarkersBeforeCheckboxes;
	}

	public Reactive getReactive() {
		return this.reactive;
	}

	public Servlet getServlet() {
		return this.servlet;
	}

	public static class Servlet {

		/**
		 * Content-Type value written to HTTP responses.
		 */
		private MimeType contentType = MimeType.valueOf("text/html");

		/**
		 * Whether Thymeleaf should start writing partial output as soon as possible or
		 * buffer until template processing is finished.
		 */
		private boolean producePartialOutputWhileProcessing = true;

		public MimeType getContentType() {
			return this.contentType;
		}

		public void setContentType(MimeType contentType) {
			this.contentType = contentType;
		}

		public boolean isProducePartialOutputWhileProcessing() {
			return this.producePartialOutputWhileProcessing;
		}

		public void setProducePartialOutputWhileProcessing(boolean producePartialOutputWhileProcessing) {
			this.producePartialOutputWhileProcessing = producePartialOutputWhileProcessing;
		}

	}

	public static class Reactive {

		/**
		 * Maximum size of data buffers used for writing to the response. Templates will
		 * execute in CHUNKED mode by default if this is set.
		 */
		private DataSize maxChunkSize = DataSize.ofBytes(0);

		/**
		 * Media types supported by the view technology.
		 */
		private List<MediaType> mediaTypes;

		/**
		 * Comma-separated list of view names (patterns allowed) that should be executed
		 * in FULL mode even if a max chunk size is set.
		 */
		private String[] fullModeViewNames;

		/**
		 * Comma-separated list of view names (patterns allowed) that should be the only
		 * ones executed in CHUNKED mode when a max chunk size is set.
		 */
		private String[] chunkedModeViewNames;

		public List<MediaType> getMediaTypes() {
			return this.mediaTypes;
		}

		public void setMediaTypes(List<MediaType> mediaTypes) {
			this.mediaTypes = mediaTypes;
		}

		public DataSize getMaxChunkSize() {
			return this.maxChunkSize;
		}

		public void setMaxChunkSize(DataSize maxChunkSize) {
			this.maxChunkSize = maxChunkSize;
		}

		public String[] getFullModeViewNames() {
			return this.fullModeViewNames;
		}

		public void setFullModeViewNames(String[] fullModeViewNames) {
			this.fullModeViewNames = fullModeViewNames;
		}

		public String[] getChunkedModeViewNames() {
			return this.chunkedModeViewNames;
		}

		public void setChunkedModeViewNames(String[] chunkedModeViewNames) {
			this.chunkedModeViewNames = chunkedModeViewNames;
		}

	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019 年 10 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
SpringBoot2.x系列教程(三十四)Thymeleaf自动配置源码解析
在之前的章节中我们已经学习了SpringBoot中Thymeleaf的基本使用,按照老规矩,我们最后来看一下Thymeleaf在SpringBoot中的自动配置相关源码。
程序新视界
2020/02/13
6170
江帅帅:精通 Spring Boot 系列 04
使用 Spring Boot 实现 Web 开发更加便捷了,因为直接依赖 spring-boot-starter-web 模块即可支持 Web 开发,此模块预定义了 Web 开发中常用的依赖包,还有内嵌的 Tomcat 作为默认 Web 容器。
江帅帅
2020/06/16
5990
使用Spring Boot开发Web项目
按:最近公众号文章主要是整理一些老文章,以个人CSDN上的博客为主,也会穿插一些新的技术点。 ---- 前面两篇博客中我们简单介绍了Spring Boot项目的创建、并且也带小伙伴们来DIY了一个Spring Boot自动配置功能,那么这些东西说到底最终还是要回归到Web上才能体现出它的更大的价值,so,今天我们就来看一下如何使用Spring Boot来开发Web项目。当然,如果小伙伴对Spring Boot尚不熟悉的话,可以先参考一下这两篇博客: 1.初识Spring Boot框架 2.初识Spring
江南一点雨
2018/04/02
1K0
使用Spring Boot开发Web项目
Thymeleaf引擎支持Multi Prefix
最近团队的一个项目在重构,希望引入Thymeleaf减少页面端的代码复杂性。在重构过程中,发现html文件需要保存在多个不同的目录中,但Thymeleaf缺省的实现不支持这种方式。
程序猿讲故事
2019/09/27
1.1K0
Thymeleaf引擎支持Multi Prefix
Spring Cloud 2.x系列之模板引擎thymeleaf
相对html+js的传统设计,现在很多网站都采用div&css+标签化+模块化的设计。模板引擎根据一定的语义,将数据填充到模板中,产生最终的HTML页面。模板引擎主要分两种:客户端引擎和服务端引擎。
BUG弄潮儿
2022/06/30
7830
Spring Cloud 2.x系列之模板引擎thymeleaf
_SpringBoot自带模板引擎Thymeleaf使用详解②
thymeleaf将遍历的状态变量封装到一个对象中,通过该对象的属性可以获取状态变量:
会洗碗的CV工程师
2023/11/17
1850
_SpringBoot自带模板引擎Thymeleaf使用详解②
Spring Boot整合Thymeleaf模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML、XHTML、HTML5内容的模板引擎。类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。 Thymeleaf也是Spring Boot首要支持的模板引擎,并且在最新的Spring Boot版本中已经不再支持Velocity了。 官网:http://www.thymeleaf.org/ 引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖。
Java技术栈
2018/03/30
9330
Spring Boot整合Thymeleaf模板引擎
Spring Boot 整合 Thymeleaf 完整 Web 案例
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢!
二哥聊运营工具
2021/12/17
1.8K0
Spring Boot 整合 Thymeleaf 完整 Web 案例
Thymeleaf一篇就够了
大家好,我是bigsai,今天我们来学习Thymeleaf,如果你对Thymeleaf比较陌生也不要紧,它很容易学习与理解,并有着自己鲜明的特色。
bigsai
2020/07/29
2.6K1
SpringBoot整合Thymeleaf
可能在开发过程中,大家会觉得每次更改页面后,都要重新重启服务,很是麻烦与反人类,可以通过配置热启动来改善(即上面的写法:spring.thymeleaf.cache=false)
用户10198968
2022/12/21
1.4K1
Spring Boot2(五):使用Spring Boot结合Thymeleaf模板引擎使用总结
一般来说,常用的模板引擎有JSP、Velocity、Freemarker、Thymeleaf 。
鸟不拉屎
2019/07/03
3K1
【Spring Boot】007-Spring Boot Web开发:静态资源导入、Thymeleaf使用
XXXAutoConfiguration:像容器中自动配置组件(Spring Boot帮我们配置的内容);
訾博ZiBo
2025/01/06
1490
【Spring Boot】007-Spring Boot Web开发:静态资源导入、Thymeleaf使用
Thymeleaf从入门到清晰使用
但是:springboot这个项目首先是以jar的方式,不是war,第二,我们用的还是嵌入式的Tomcat,所以他默认是不支持jsp,对此springboot推荐我们使用Thymeleaf模板引擎
秋名山码神
2023/01/13
1K0
Thymeleaf从入门到清晰使用
【springmvc thymeleaf】springmvc整合thymeleaf
Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品。
lomtom
2021/10/27
2.8K0
【springmvc thymeleaf】springmvc整合thymeleaf
深入Spring Boot (八):模板引擎使用详解
《深入Spring Boot (六):使用SpringMVC框架创建Web应用》示例代码创建的是REST web服务,Spring MVC除了可以实现REST web服务之外,还可以使用它提供动态HTML内容。Spring MVC支持多种模板技术,包括Thymeleaf、FreeMarker和JSPs。另外,许多其他的模板引擎也包括他们自己与Spring MVC的集成使用。Spring Boot支持以下模板引擎的自动配置: FreeMarker Groovy Thymeleaf Mustache 需要注意的
JavaQ
2018/04/08
1.5K0
深入Spring Boot (八):模板引擎使用详解
SpringBoot | SpringBoot Web开发
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3C8F9icU-1662430755757)(https://cdn.jsdelivr.net/gh/BoBooY-GitHub/SpringBoot_NoteImgs@main/imgs/202209041108838.png)]
啵啵鱼
2022/11/23
1.1K0
SpringBoot | SpringBoot Web开发
Spring Boot实战:模板引擎
  虽然现在很多开发,都采用了前后端完全分离的模式,即后端只提供数据接口,前端通过AJAX请求获取数据,完全不需要用的模板引擎。这种方式的优点在于前后端完全分离,并且随着近几年前端工程化工具和MVC框架的完善,使得这种模式的维护成本相对来说也更加低一点。但是这种模式不利于SEO,并且在性能上也会稍微差一点,还有一些场景,使用模板引擎会更方便,比如说邮件模板。这篇文章主要讨论Spring boot与模板引擎Thymeleaf、Freemaker以及JSP的集成。 一、集成Thymeleaf 第一步:引入j
用户2140019
2018/05/18
1.3K0
(五) SpringBoot起飞之路-Thymeleaf模板引擎整合及基本用法总结
现在来说,前后端分离开始更加流行,但是很多旧的项目,或者自己一个人写东西,我感觉使用模板引擎也是非常不错的选择,还有时候去找一些后台的开源模板,有一些也都用了Thymeleaf, 何况出于学习的态度,学哪种技术都是可以的
BWH_Steven
2020/05/31
2.2K0
SpringBoot之SpringBoot整合Thymeleaf模板引擎
  注意:不明白Flower的可以看一下《SpringBoot之读取配置文件中自定义的值》,这次放入一个对象进去
彼岸舞
2021/01/26
3680
极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板,例如邮件发送模板)。
江南一点雨
2019/06/18
1.4K0
相关推荐
SpringBoot2.x系列教程(三十四)Thymeleaf自动配置源码解析
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验