您的位置:首页 > 编程语言 > Java开发

springboot thymeleaf 生成静态html

2018-07-13 11:10 1586 查看
1.引入jar

<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.6</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.模板文件(位置:resources/static/tempfiles)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>欢迎页</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport" />
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body>
<div class="panel panel-default">
<div class="panel-heading">你好,<a th:text="${name}"></a></div>
<div style="padding: 10px 0 20px 10px;">

</div>
</div>
</body>
</html>

3..在spring启动类中测试下

@SpringBootApplication
public class DemoStaticApplication {

public static void main(String[] args) {
SpringApplication.run(DemoStaticApplication.class, args);

final String TEMPLATE_PREFIX = "static/tempfiles/";
final String TEMPLATE_SUFFIX = ".html";//

try {
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix(TEMPLATE_PREFIX);
resolver.setSuffix(TEMPLATE_SUFFIX);
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);

//填充数据
Context context = new Context();
context.setVariable("name", "俩快线-俩快线");
//渲染模板生成静态
FileWriter writer = new FileWriter("d:/html/index.html");
templateEngine.process("temp", context, writer);
} catch (Exception e) {
System.out.println(e);
}
}
}

搞定收工!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息