Skip to content

Commit 6a96cb3

Browse files
committed
springboot-thymeleaf-static
1 parent c6022e0 commit 6a96cb3

File tree

9 files changed

+214
-0
lines changed

9 files changed

+214
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-thymeleaf-static/pom.xml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.tellsea</groupId>
12+
<artifactId>springboot-thymeleaf-static</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-thymeleaf-static</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-maven-plugin</artifactId>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.tellsea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootThymeleafStaticApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootThymeleafStaticApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cn.tellsea.service;
2+
3+
public interface ThymeleafService {
4+
5+
void createHtml(Long id);
6+
7+
void deleteHtml(Long id);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cn.tellsea.service.impl;
2+
3+
import cn.tellsea.service.ThymeleafService;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
import org.thymeleaf.TemplateEngine;
8+
import org.thymeleaf.context.Context;
9+
10+
import java.io.File;
11+
import java.io.PrintWriter;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
@Slf4j
16+
@Service
17+
public class ThymeleafServiceImpl implements ThymeleafService {
18+
19+
public static final String destPath = "D:/temp/static";
20+
21+
@Autowired
22+
private TemplateEngine templateEngine;
23+
24+
public Map<String, Object> loadModel(Long id) {
25+
// 讲道理,这里加载的数据是根据id从数据库查询出来的,我这里就写固定了
26+
Map<String, Object> map = new HashMap<>();
27+
map.put("name", "tellsea");
28+
map.put("age", 20);
29+
map.put("email", "3210054449@qq.com");
30+
return map;
31+
}
32+
33+
/**
34+
* 创建html页面
35+
*
36+
* @param spuId
37+
* @throws Exception
38+
*/
39+
public void createHtml(Long spuId) {
40+
// 上下文
41+
Context context = new Context();
42+
context.setVariables(loadModel(spuId));
43+
// 输出流
44+
File dest = new File(destPath, spuId + ".html");
45+
if (dest.exists()) {
46+
dest.delete();
47+
}
48+
try (PrintWriter writer = new PrintWriter(dest, "UTF-8")) {
49+
// 生成html
50+
templateEngine.process("id", context, writer);
51+
} catch (Exception e) {
52+
log.error("[静态页服务]:生成静态页异常", e);
53+
}
54+
}
55+
56+
public void deleteHtml(Long id) {
57+
// 输出流
58+
File dest = new File(destPath, id + ".html");
59+
if (dest.exists()) {
60+
dest.delete();
61+
}
62+
}
63+
}

springboot-thymeleaf-static/src/main/resources/application.yml

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Thymeleaf 静态页面</title>
6+
</head>
7+
<body>
8+
<h1 th:text="'名字是:' + ${name}"></h1>
9+
<h1 th:text="'年龄是:' + ${age}"></h1>
10+
<h1 th:text="'邮箱是:' + ${email}"></h1>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.tellsea;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringbootThymeleafStaticApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.tellsea.service;
2+
3+
import cn.tellsea.SpringbootThymeleafStaticApplicationTests;
4+
import org.junit.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
public class ThymeleafServiceTest extends SpringbootThymeleafStaticApplicationTests {
8+
9+
@Autowired
10+
private ThymeleafService thymeleafService;
11+
12+
@Test
13+
public void createHtmlTest() {
14+
thymeleafService.createHtml(1L);
15+
}
16+
17+
@Test
18+
public void delete() {
19+
thymeleafService.deleteHtml(1L);
20+
}
21+
}

0 commit comments

Comments
 (0)