부제: Thymeleaf 이용해서 HTML 파일 저장하기
HTML 파일 형식의 보고서(Report)를 만들어야 해서
Spring Boot 에서 Thymeleaf 를 이용하여 HTML 파일을 저장하려 한다.
아래와 같이 작업을 했다.
디렉토리 구조
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>kr.co._1004lucifer.test.tymeleaf</groupId>
<artifactId>savefile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Save HTML File</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Java 소스
package kr.co._1004lucifer.test.tymeleaf.savefile;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
@SpringBootTest
class ThymeleafFileWriterTest {
@Autowired
TemplateEngine templateEngine;
/**
* Thymeleaf를 이용해서 HTML 파일을 저장시키는 테스트
*/
@Test
public void writeThymeleaf2HtmlFile() throws IOException {
String filepath = System.getProperty("user.home") + "/hello-thymeleaf.html";
Context context = new Context();
context.setVariable("title", "User Page");
context.setVariable("name", "John Doe");
Writer writer = new FileWriter(filepath);
writer.write(templateEngine.process("sample.html", context));
writer.close();
File file = new File(filepath);
Assertions.assertTrue(file.length() > 0);
Assertions.assertTrue(file.delete());
}
}
HTML 템플릿 파일
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.thymeleaf.org ">
<head>
<title th:text="${title}">title</title>
</head>
<body>
Hello <span th:text="${name}">name</span>!
</body>
</html>
위의 테스트 코드는 github 저장소에 올려놓았다.
https://github.com/1004lucifer/Test_SpringBoot-Thymeleaf-Save-File
save, write, file
타임리프
참고
- https://jgalacambra.wordpress.com/2016/06/08/writing-a-file-using-thymeleaf/

댓글
댓글 쓰기