页面静态化FreeMarker的使用

什么是页面静态化

在访问 新闻。活动,商品 详情页面,路径可是是xx[id].html,服务器端根据请求id,动态生成html网页,下次访问数据时,无需在查询数据,直接将html静态页面返回=====减少数据库交互,提高查询性能,结合Freemarker模板技术----生成html

FreeMarker的使用

freemark实现自定义标签模板

模板文件+java数据对象====输出(任何格式文本)
freemark模板文件 通常扩展名为.ftl

FreeMarker的入门

首先要引入FreeMarker的jar包
在这里插入图片描述
测试
hello.ftl

<html>
	<title>
		${title}
	</title>
	<body>
		${msg}
	</body>
	
</html>

测试用例

//配置对象,配置模板位置
		Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
		//获取模板文件w位置
		configuration.setDirectoryForTemplateLoading(new File("src/main/webapp/WEB-INF/template"));
		//获取模板对象
		Template template = configuration.getTemplate("hello.ftl");
		//动态数据对象
		Map<String, Object> paramterMap = new HashMap<String,Object>();
		paramterMap.put("title","测试");
		paramterMap.put("msg", "demo");
		//合并输出到控制台
		template.process(paramterMap, new PrintWriter(System.out));

结果

在这里插入图片描述

总结

FreeMarker可以实现页面静态化,减少数据库的查询,提高查询效率

猜你喜欢

转载自blog.csdn.net/weixin_42392859/article/details/85448401