SpringBoot如何用java生成静态html
作者:??七号im???? 时间:2023-08-08 08:46:59
SpringBoot集成Freemarker
主要特征:静态页面,无接 * 互
数据实时性不高且体量小的网站可采用生成静态html的形式
数据提前渲染至html内,若发生数据更新,则重新渲染数据
CDN加速让网站不再龟速
1. 引入Maven依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
2. 创建ftl
<html>
<head>
<title>啦啦啦啦啦</title>
</head>
<body>
<h1>侠客行</h1>
<p>${author!}</P>
<#if (poem?size)!=0>
<#list poem as item>
<p>${item.first!}${item.second!}</p></br>
</#list>
</#if>
</body>
</html>
3. 创建freeMarker工具类
@Slf4j
@Component
public class FreeMarkerUtil {
private static Configuration config;
private static String serverPath;
@Value("${spring.servlet.multipart.location:D:/static/}")
public void setServerPath(String serverPath) {
FreeMarkerUtil.serverPath = serverPath;
}
/**
* 通过freemarker生成静态HTML页面
* @param templateName 模版名称
* @param targetFileName 生成后的文件名
* @param ftlPath模板路径
* @param htmlPathhtml路径
* @param mapfreemarker生成的数据都存储在MAP中,
*/
public static void createHtml(String templateName, String targetFileName, String ftlPath, String htmlPath, Map<String, Object> map) {
try{
//创建fm的配置
config = new Configuration();
//指定默认编码格式
config.setDefaultEncoding("UTF-8");
//设置模版文件的路径
config.setDirectoryForTemplateLoading(new File(serverPath+ftlPath));
//获得模版包
Template template = config.getTemplate(templateName);
//从参数文件中获取指定输出路径
String path = serverPath+htmlPath;
//生成的静态页存放路径如果不存在就创建
File file = null;
file=new File(path);
if (!file.exists()){
file.mkdirs();
}
//定义输出流,注意必须指定编码
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path+"/"+targetFileName)), UTF_8));
//生成模版
template.process(map, writer);
}catch (Exception e){
log.error("生成异常:{}",e);
}
}
4. 编写Java的代码
构造实体类,通过freemarker将实体类的信息渲染至html
@GetMapping("test")
public Object test() {
Map<String,Object> map = new HashMap<>(16);
List<Poem> list = new ArrayList<>();
list.add(new Poem("赵客缦胡缨,", "吴钩霜雪明。"));
list.add(new Poem("银鞍照白马,", "飒沓如流星。"));
list.add(new Poem("十步杀一人,", "千里不留行。"));
list.add(new Poem("事了拂衣去,", "深藏身与名。"));
map.put("author","李白");
map.put("poem",list);
FreeMarkerUtil.createHtml("poem.ftl","poem.html","侠客行/","侠客行/",map);
return BackMessage.ok(map);
}
实体类:
@Data
public class Poem {
private String first;
private String second;
public Poem(String first, String second) {
this.first = first;
this.second = second;
}
}
5. Html输出
<html>
<head>
<title>啦啦啦啦啦</title>
</head>
<body>
<h1>侠客行</h1>
<p>李白</P>
<p>赵客缦胡缨,吴钩霜雪明。</p></br>
<p>银鞍照白马,飒沓如流星。</p></br>
<p>十步杀一人,千里不留行。</p></br>
<p>事了拂衣去,深藏身与名。</p></br>
</body>
</html>
来源:https://juejin.cn/post/7114473059109011493
标签:SpringBoot,java,生成,静态,html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入剖析Android消息机制原理
2023-09-30 01:57:11
![](https://img.aspxhome.com/file/2023/6/85966_0s.jpg)
Spring Boot整合Lombok的方法详解
2023-11-22 09:06:21
![](https://img.aspxhome.com/file/2023/7/85497_0s.jpg)
使用cmd根据WSDL网址生成java客户端代码的实现
2022-09-12 11:00:20
![](https://img.aspxhome.com/file/2023/1/99871_0s.jpg)
详解Spring Boot Profiles 配置和使用
2021-10-05 22:54:57
List集合多个复杂字段判断去重的案例
2022-08-01 16:23:28
SpringBoot+Netty+WebSocket实现消息发送的示例代码
2023-08-16 00:02:52
解决Android Studio电脑不支持HAXM的问题
2022-05-28 04:48:51
![](https://img.aspxhome.com/file/2023/4/108314_0s.jpg)
java实现秒表功能
2023-07-06 05:55:13
![](https://img.aspxhome.com/file/2023/7/80877_0s.jpg)
WinForm自定义函数FindControl实现按名称查找控件
2021-09-23 17:27:25
详解SpringMVC实现图片上传以及该注意的小细节
2022-04-29 08:34:30
Android仿腾讯视频实现悬浮窗效果
2021-06-16 00:49:23
![](https://img.aspxhome.com/file/2023/8/110728_0s.gif)
gson对象序列化的示例
2023-11-25 08:54:28
![](https://img.aspxhome.com/file/2023/6/59956_0s.png)
Android RollPagerView实现轮播图
2023-06-12 21:10:32
解决SpringBoot webSocket 资源无法加载、tomcat启动报错的问题
2021-07-28 05:06:42
MyBatis通用的10种写法总结大全
2022-08-01 12:04:02
使用Maven Helper解决Maven插件冲突的方法
2023-11-08 06:07:05
![](https://img.aspxhome.com/file/2023/1/58751_0s.jpg)
sort page 排序和分页的小例子
2022-07-31 23:08:37
java编译器和JVM的区别
2023-07-18 20:34:48
详解Alibaba Java诊断工具Arthas查看Dubbo动态代理类
2021-08-04 03:16:28
![](https://img.aspxhome.com/file/2023/4/64064_0s.png)
C# List实现行转列的通用方案
2022-03-28 02:53:04
![](https://img.aspxhome.com/file/2023/4/75614_0s.png)