博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Freemarker网页静态化
阅读量:4579 次
发布时间:2019-06-09

本文共 2136 字,大约阅读时间需要 7 分钟。

Freemarker的jar包

创建整合spring的配置文件

 

org.freemarker
freemaeker
2.3.23

  

@Autowired	private FreeMarkerConfigurer freeMarkerConfigurer;	@RequestMapping("/genhtml")	@ResponseBody	public String genHtml()throws Exception {		// 1、从spring容器中获得FreeMarkerConfigurer对象。		// 2、从FreeMarkerConfigurer对象中获得Configuration对象。		Configuration configuration = freeMarkerConfigurer.getConfiguration();		// 3、使用Configuration对象获得Template对象。		Template template = configuration.getTemplate("hello.ftl");		// 4、创建数据集		Map dataModel = new HashMap<>();		dataModel.put("hello", "1000");		// 5、创建输出文件的Writer对象。		Writer out = new FileWriter(new File("D:/temp/term197/out/spring-freemarker.html"));		// 6、调用模板对象的process方法,生成文件。		template.process(dataModel, out);		// 7、关闭流。		out.close();		return "OK";	}

 

网页的静态化方案

输出文件的名称:商品id+.html

输出文件的路径:工程外部的任意目录。

网页访问:使用nginx访问网页。在此方案下tomcat只有一个作用就是生成静态页面。

 

生成静态页面的时机:商品添加后,生成静态页面。可以使用Activemq,订阅topic(商品添加)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

      xmlns:context="http://www.springframework.org/schema/context"

      xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

      <bean id="freemarkerConfig"

            class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

            <property name="templateLoaderPath" value="/WEB-INF/ftl/" />

            <property name="defaultEncoding" value="UTF-8" />

      </bean>

 

 

</beans>

转载于:https://www.cnblogs.com/zhoucx66/p/9322135.html

你可能感兴趣的文章
【题解】Luogu P2319 [HNOI2006]超级英雄
查看>>
【题解】Luogu P4324 [JSOI2016]扭动的回文串
查看>>
1038: 亲和数
查看>>
Lintcode: Topological Sorting
查看>>
布局视口,完美视口,
查看>>
带命令行参数的main函数详解
查看>>
Linux driver 原子操作-管理设备被多个文件(进程)打开
查看>>
Java小记
查看>>
一些通过SAP ABAP代码审查得出的ABAP编程最佳实践
查看>>
SAP Cloud for Customer(C4C)和微信集成系列教程
查看>>
最简单的dockerfile使用教程 - 创建一个支持SSL的Nginx镜像
查看>>
读史——秦历史概况
查看>>
Proxy代理对象是如何调用invoke()方法的.
查看>>
iOS 9之SFSafariViewController
查看>>
Sql Server 游标例子笔记
查看>>
HDU 1717(sscanf的应用)
查看>>
UVA 10041 - Vito's Family
查看>>
王小呆的模拟赛
查看>>
2017-2018-2 20155231《网络对抗技术》实验五: MSF基础应用
查看>>
字节 & 字符 & 位
查看>>