在Spring中配置Thymeleaf的方法

本文介绍如何在Spring框架中配置Thymeleaf作为HTML5模板引擎,并提供详细的配置步骤,包括所需依赖Jar包及SpringMVC配置文件的具体设置。

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

最近在学习Spring框架,偶尔看到Thymeleaf这一HTML模板,支持HTML5,性能和编写都很容易,以后就用它替换JSP了,下面是它在Spring中的配置方式:

1.导入依赖的Jar包:

ognl,slf4j,javassist,thyleaf,thyleaf-spring;

2.在SpringMVC的配置文件servlet-context.xml中配置Thyleaf的视图解析器:

        <!-- thymeleaf的视图解析器 -->
	<beans:bean id="templateResolver"
		class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
		<beans:property name="prefix" value="/WEB-INF/views/html" />
		<beans:property name="suffix" value=".html" />
		<beans:property name="order" value="0"/>
		<beans:property name="templateMode" value="HTML5" />
	</beans:bean>
	<beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
		<beans:property name="templateResolver" ref="templateResolver" />
	</beans:bean>
	<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
		<beans:property name="templateEngine" ref="templateEngine" />
		<beans:property name="characterEncoding" value="UTF-8"/>
	</beans:bean>

## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 attrRequestScope不为空显示 ~~~attrRequestScope为空显示~~~~ ``` #### 2. 测试循环 ```html 测试循环 ``` > 1. 使用th:each进行集合数据迭代 > 2. th:each="声明变量:${集合}" > 3. th:each 用在哪个标签上,哪个标签就会出现多次 ### 7. 引入外部代码 1. 要被引入的代码 include/part.html ```html 被包含的内容1111 被包含的内容2222 被包含的内容3333 ``` 2. 需要引入的页面 ```html ``` 3. 渲染后的页面源码 ```html 被包含的内容1111 被包含的内容2222 被包含的内容3333 ``` > 1. " :: "左边的值拼前后缀后必须能够找到要包含的文件。 > 2. " :: " 右边的值是代码片段的名字 ,就是th:fragment的值。 > 3. insert将代码原样引入。 > 4. replace使用被引入的代码和属性替换原有的。 > 5. include使用被引入的代码div内部的代码。
### Spring Cloud 中 Thymeleaf 配置与静态资源打包 在 Spring Cloud 项目中,正确配置 Thymeleaf 模板引擎并确保静态资源能够被打包是一项常见的需求。以下是详细的说明: #### 1. 添加依赖项 为了支持 Thymeleaf 和静态资源的处理,在 `pom.xml` 文件中需引入必要的 Maven 依赖项。 ```xml <dependencies> <!-- Spring Boot Web Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Thymeleaf Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Static Resource Support (Optional, usually included by default) --> <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> </dependency> </dependencies> ``` 以上代码片段展示了如何通过 Maven 引入 Thymeleaf 及其相关组件[^3]。 --- #### 2. 配置 Thymeleaf 参数 在项目的 `application.properties` 或 `application.yml` 文件中,可以自定义 Thymeleaf 的行为参数。例如缓存设置、编码方式等。 ##### 使用 application.properties 进行配置: ```properties # 开发环境建议关闭缓存以便实时查看效果 spring.thymeleaf.cache=false # 设置字符集为 UTF-8 spring.thymeleaf.encoding=UTF-8 # 定义模板前缀和后缀路径 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` ##### 使用 application.yml 进行配置: ```yaml spring: thymeleaf: cache: false encoding: UTF-8 prefix: classpath:/templates/ suffix: .html ``` 这些配置确保了 Thymeleaf 能够正常加载 HTML 模板文件,并适配不同的运行环境[^4]。 --- #### 3. 处理静态资源 Spring Boot 默认会自动扫描某些目录下的静态资源文件(如 CSS、JavaScript、图片等),并将它们映射到特定 URL 下。默认情况下,以下目录会被识别为静态资源根目录: - `/src/main/resources/static/` - `/src/main/resources/public/` - `/src/main/resources/META-INF/resources/` 如果需要额外指定其他静态资源路径,则可以在 `application.properties` 或 `application.yml` 中扩展配置。 ##### 自定义静态资源路径: ```properties spring.resources.static-locations=classpath:/static/,classpath:/custom-resources/ ``` 或者使用 YAML 格式: ```yaml spring: resources: static-locations: classpath:/static/,classpath:/custom-resources/ ``` 这样就可以将多个目录中的静态资源统一管理起来[^5]。 --- #### 4. 打包静态资源 当使用 Maven 构建工具时,默认会将位于上述静态资源目录内的文件复制至最终生成的 JAR/WAR 包中。因此无需特别操作即可完成静态资源的打包过程。 不过需要注意的是,如果存在复杂的前端工程结构(例如 Vue.js 或 React 应用程序嵌套于 Java 后端之中),则可能需要借助构建工具(如 Webpack)先编译前端代码再将其产物放入对应的静态资源目录下。 --- #### 5. 测试功能 创建一个简单的控制器来测试 Thymeleaf 渲染以及静态资源配置是否生效。 ```java package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class IndexController { @GetMapping("/") public String index(Model model) { model.addAttribute("message", "Welcome to the Spring Cloud with Thymeleaf!"); return "index"; } } ``` 同时准备一份名为 `index.html` 的模板文件放置于 `resources/templates/` 目录下: ```html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"> <head> <title>Home Page</title> <link rel="stylesheet" type="text/css" href="/css/style.css"/> </head> <body> <h1 th:text="${message}">Default Message</h1> <p>This is a paragraph.</p> <script src="/js/app.js"></script> </body> </html> ``` 最后确认样式表 (`style.css`) 和脚本文件 (`app.js`) 已经存在于 `resources/static/css/` 和 `resources/static/js/` 子目录里[^6]。 --- ### 总结 按照上述方法可顺利完成 Spring Cloud 项目中关于 Thymeleaf 模板引擎及其关联静态资源的相关配置工作。整个流程涵盖了从基础依赖导入直至实际应用层面的具体实现细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值