thymeleaf网页静态化
时间: 2023-10-13 09:02:50 浏览: 111
Thymeleaf 是一个Java模板引擎,可以用于在服务器端将动态内容渲染到网页上。而网页静态化是将动态生成的网页转化为静态的HTML文件,以提高网站的访问速度和减轻服务器压力。
在使用Thymeleaf进行网页静态化时,可以通过以下步骤实现:
1. 在项目中引入Thymeleaf依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 创建Thymeleaf模板文件,例如index.html:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>静态化页面</title>
</head>
<body>
<div th:text="${message}">动态内容</div>
</body>
</html>
```
3. 在控制器中设置动态内容并渲染静态页面:
```java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/static")
public class StaticController {
@GetMapping("/generate")
public String generateStaticPage(Model model) {
model.addAttribute("message", "Hello, Thymeleaf!");
return "index"; // 返回模板文件名
}
}
```
4. 在应用启动类上添加@E
阅读全文
相关推荐


















