目錄
一、添加依賴
1.1首先,在項(xiàng)目的構(gòu)建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依賴。例如,對(duì)于 Maven 項(xiàng)目,在 pom.xml 文件中添加以下依賴
1.2保存并更新項(xiàng)目依賴
二、配置Thymeleaf
2.1模板位置配置
2.2模板緩存配置
2.3自定義標(biāo)簽配置
三、創(chuàng)建模板文件
3.1創(chuàng)建一個(gè)HTML文件
3.2在HTML文件中引入Thymeleaf命名空間
3.3在HTML文件中使用Thymeleaf語(yǔ)法來(lái)定義模板內(nèi)容
3.4在Java代碼中加載模板,并將數(shù)據(jù)傳遞給模板
3.5將生成的HTML代碼響應(yīng)給客戶端
四、控制器中使用Thymeleaf
4.1在Spring Boot中,在pom.xml文件中添加以下依賴項(xiàng)
4.2在Spring MVC控制器類(lèi)中,使用@Controller注解標(biāo)記該類(lèi),并使用@RequestMapping注解定義處理請(qǐng)求的方法。
4.3創(chuàng)建一個(gè)包含Thymeleaf模板的HTML文件,并將其放置在/resources/templates目錄下。
4.4運(yùn)行
五、在模板中使用Thymeleaf語(yǔ)法
5.1輸出變量值
5.2判斷條件
5.3循環(huán)迭代
5.4設(shè)置屬性
5.5表單處理
總結(jié)
一、添加依賴
1.1首先,在項(xiàng)目的構(gòu)建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依賴。例如,對(duì)于 Maven 項(xiàng)目,在 pom.xml 文件中添加以下依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2保存并更新項(xiàng)目依賴
這樣就完成了Thymeleaf的引入。在你的代碼中,你可以使用Thymeleaf提供的標(biāo)簽和表達(dá)式來(lái)處理模板中的動(dòng)態(tài)內(nèi)容,并將其渲染為最終的HTML頁(yè)面。
二、配置Thymeleaf
2.1模板位置配置
可以通過(guò)配置?spring.thymeleaf.prefix
?和?spring.thymeleaf.suffix
?來(lái)指定模板文件的位置和后綴
spring.thymeleaf.prefix=/WEB-INF/templates/
spring.thymeleaf.suffix=.html
上述配置將會(huì)使 Thymeleaf 在 /WEB-INF/templates/
目錄下查找以 .html
結(jié)尾的模板文件。
2.2模板緩存配置
Thymeleaf 默認(rèn)開(kāi)啟了模板緩存,以提高性能。在開(kāi)發(fā)階段可能需要關(guān)閉緩存以方便調(diào)試,可以通過(guò)配置?spring.thymeleaf.cache
?進(jìn)行設(shè)置。
spring.thymeleaf.cache=false
上述配置將會(huì)使Thymeleaf的模板緩存
2.3自定義標(biāo)簽配置
Thymeleaf 支持自定義標(biāo)簽,可以在配置中注冊(cè)自定義標(biāo)簽處理器。
@Configuration
public class ThymeleafConfig implements ITemplateResolver {
// ...其他配置
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
// 配置模板位置、緩存等
// ...
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// 注冊(cè)自定義標(biāo)簽處理器
// ...
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
// 其他配置
// ...
return viewResolver;
}
// ...其他配置
}
上述代碼中,通過(guò) templateResolver()
方法配置模板解析器,通過(guò) templateEngine()
方法配置模板引擎,最后通過(guò) viewResolver()
方法配置視圖解析器。在 templateResolver()
和 templateEngine()
方法中可以設(shè)置一些自定義的屬性,如模板位置、緩存等。在 templateEngine()
方法中還可以注冊(cè)自定義的標(biāo)簽處理器
三、創(chuàng)建模板文件
3.1創(chuàng)建一個(gè)HTML文件
將其命名為模板名稱(chēng),如“template.html"。
3.2在HTML文件中引入Thymeleaf命名空間
<h1 th:text="${title}">Page Title</h1>
<p th:text="${content}">Page Content</p>
3.3在HTML文件中使用Thymeleaf語(yǔ)法來(lái)定義模板內(nèi)容
<h1 th:text="${title}">Page Title</h1>
<p th:text="${content}">Page Content</p>
3.4在Java代碼中加載模板,并將數(shù)據(jù)傳遞給模板
// 加載模板文件
Template template = thymeleafTemplateEngine.getTemplate("template.html");
// 創(chuàng)建一個(gè)上下文對(duì)象,用于傳遞數(shù)據(jù)給模板
Context context = new Context();
context.setVariable("title", "My Page Title");
context.setVariable("content", "Hello, world!");
// 渲染模板并生成HTML代碼
String renderedHtml = templateEngine.process(template, context);
在上面的例子中,我們使用Thymeleaf模板引擎加載模板文件"template.html",然后創(chuàng)建一個(gè)上下文對(duì)象并通過(guò)它將數(shù)據(jù)傳遞給模板。最后,我們調(diào)用process()
方法來(lái)渲染模板,并生成HTML代碼。
3.5將生成的HTML代碼響應(yīng)給客戶端
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(renderedHtml);
四、控制器中使用Thymeleaf
4.1在Spring Boot中,在pom.xml文件中添加以下依賴項(xiàng)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
4.2在Spring MVC控制器類(lèi)中,使用@Controller注解標(biāo)記該類(lèi),并使用@RequestMapping注解定義處理請(qǐng)求的方法。
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello(Model model) {
model.addAttribute("message", "Hello, Thymeleaf!");
return "hello";
}
}
4.3創(chuàng)建一個(gè)包含Thymeleaf模板的HTML文件,并將其放置在/resources/templates目錄下。
例如,創(chuàng)建一個(gè)名為"hello.html"的文件。在模板中使用Thymeleaf的語(yǔ)法來(lái)渲染數(shù)據(jù)。例如,使用${message}
獲取在控制器中添加到Model的屬性。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello Thymeleaf</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
4.4運(yùn)行
運(yùn)行應(yīng)用程序,并訪問(wèn)http://localhost:8080/hello
,即可看到渲染后的頁(yè)面。
五、在模板中使用Thymeleaf語(yǔ)法
5.1輸出變量值
使用${}
表達(dá)式來(lái)輸出變量的值。
<p th:text="${message}"></p>
5.2判斷條件
使用th:if
和th:else
來(lái)實(shí)現(xiàn)條件判斷。
<p th:if="${user.isAdmin}">管理員</p>
<p th:unless="${user.isAdmin}">普通用戶</p>
5.3循環(huán)迭代
使用th:each
來(lái)遍歷集合,使用th:object
來(lái)指定迭代對(duì)象的別名。
<ul>
<li th:each="item : ${items}" th:object="${item}">
<p th:text="${name}"></p>
<p th:text="${price}"></p>
</li>
</ul>
5.4設(shè)置屬性
使用th:attr
來(lái)設(shè)置HTML元素的屬性,如href
、src
等。
<a th:href="@{/product/{id}(id=${productId})}" th:attr="title=${productName}">
<img th:src="@{${imageUrl}}" />
</a>
5.5表單處理
使用th:field
和th:errors
來(lái)綁定表單字段和錯(cuò)誤信息。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-859354.html
<form th:action="@{/login}" method="post" th:object="${user}">
<label>用戶名:<input type="text" th:field="*{username}" /></label>
<span th:errors="*{username}"></span>
<label>密碼:<input type="password" th:field="*{password}" /></label>
<span th:errors="*{password}"></span>
</form>
總結(jié)
使用Thymeleaf的步驟包括引入依賴、配置Thymeleaf、創(chuàng)建模板文件、在控制器中使用Thymeleaf和在模板中使用Thymeleaf語(yǔ)法。Thymeleaf提供了強(qiáng)大而靈活的功能,使開(kāi)發(fā)者能夠方便地實(shí)現(xiàn)數(shù)據(jù)與頁(yè)面的動(dòng)態(tài)綁定。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-859354.html
到了這里,關(guān)于前端模板引擎Thymeleaf的整合和使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!