thymeleaf的属性优先级

本文详细介绍了Thymeleaf模板引擎中标签的执行顺序,包括数字优先特性、片段包含、条件评估等功能,并解释了如何使用不同类型的属性修改器来定制HTML元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所有Thymeleaf属性定义一个数字优先,建立他们的顺序执行的标签。这个顺序是:  

OrderFeatureAttributes
1Fragment inclusionth:include
th:replace
2Fragment iterationth:each
3Conditional evaluationth:if
th:unless
th:switch
th:case
4Local variable definitionth:object
th:with
5General attribute modificationth:attr
th:attrprepend
th:attrappend
6Specific attribute modificationth:valueth:hrefth:src, etc.
7Text (tag body modification)th:text
th:utext
8Fragment specificationth:fragment
9Fragment removalth:remove

转载于:https://www.cnblogs.com/suncj/p/4031182.html

可以在Spring Boot的配置文件(application.yml或application.properties)中指定Thymeleaf的相关属性,例如: ```yaml spring: thymeleaf: cache: false # 关闭缓存 mode: HTML # 模板渲染模式,可以是HTML、XML、TEXT、JAVASCRIPT、CSS encoding: UTF-8 # 模板编码 prefix: classpath:/templates/ # 模板所在路径,可以是classpath:/templates/或file:/path/to/templates/ suffix: .html # 模板后缀 template-resolver-order: 1 # 模板解析器的优先级,数字越小优先级越高 ``` 除了在配置文件中指定属性,也可以通过Java代码配置Thymeleaf,例如: ```java @Configuration public class ThymeleafConfig { @Autowired private ApplicationContext applicationContext; @Bean public SpringResourceTemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setApplicationContext(applicationContext); templateResolver.setPrefix("classpath:/templates/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setCharacterEncoding("UTF-8"); templateResolver.setCacheable(false); return templateResolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.setEnableSpringELCompiler(true); return templateEngine; } @Bean public ThymeleafViewResolver viewResolver() { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine()); viewResolver.setCharacterEncoding("UTF-8"); viewResolver.setOrder(1); return viewResolver; } } ``` 在这个配置类中,我们定义了一个SpringResourceTemplateResolver来指定模板所在路径和文件后缀,并设置了禁用缓存、编码等属性。然后创建了一个SpringTemplateEngine并设置了模板解析器,并启用了SpringEL编译器。最后创建了一个ThymeleafViewResolver并设置了模板引擎和字符编码,并指定了解析器的优先级
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值