上一篇文章中我们讲了关于 freemarker 的基本知识和使用方法, 包括一些标签的使用, 如果没看过的童鞋, 可以点击前往学习 JAVA 学习笔记 27 网页静态化之 Freemarker(一)
那么今天我们来讲一下如何将 freemarker 和 Spring 进行整合, 并且整合后如何在代码中进行使用, gogogo!!!
1. 在 pom.xml 文件中添加依赖
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- </dependency>
2. 创建整合 spring 的配置文件
- <?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-4.2.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-4.2.xsd">
- <!-- 加载属性文件 -->
- <context:property-placeholder location="classpath:resource/resource.properties"
- />
- <!-- 配置注解驱动 -->
- <mvc:annotation-driven />
- <!-- 配置包扫描器, 扫描 @Controller 注解的类 -->
- <context:component-scan base-package="com.freemarker.controller" />
- <!-- freemarker 的配置 -->
- <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <property name="templateLoaderPath" value="/WEB-INF/ftl/" />
- <property name="defaultEncoding" value="utf-8">
- </property>
- </bean>
- </beans>
3. 创建 htmlGenController 测试 Spring 整合生成静态页面
使用步骤:
1 从 spring 容器中获得 FreeMarkerConfigurer 对象
2 从 FreeMarkerConfigurer 对象中获得 Configuration 对象
3 使用 Configuration 对象获得 Template 对象
4 创建数据集
5 创建输出文件的 Writer 对象
6 调用模板对象的 process 方法, 生成文件
7 关闭流
具体代码如下:
- @Controller
- public class HTMLGenController {
- @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 data = new HashMap<>();
- data.put("hello", "spring freemarker test");
- //5 创建输出文件的 Writer 对象
- Writer out = new FileWriter(new File("E:/temp/javaee28/out/test.html"));
- //6 使用模板对象的 process 方法输出文件
- template.process(data, out);
- //7 关闭流
- out.close();
- return "OK";
- }
- }
4. 启动服务, 测试代码
我们这里是通过服务器访问 controller 来生成静态页面的, 所以我们需要先启动服务器
然后在浏览器中输入 localhost:8080/genhtml, 然后回车, 这时候到我们指定的目录下 E:/temp/javaee28/out/test.html, 找到生成的 test.html, 双击打开
可以看到, 这个就是我们刚才使用整合后的方式生成的静态页面了
总结
关于网页静态化的使用场景远不止这些, 我们这里只做一个入门的演示作用, 这里举个例子, 比如说我们新添加了一件商品, 在我们通过 activemq 发布一个 topic 的时候, 一方面需要更新我们的商品索引库的索引, 这时候, 我们同时需要为这个商品生成一个静态页面, 我们可以通过插入的商品 id, 查询数据库得到商品详情, 然后将数据填充到 ftl 模板中, 然后生成静态页面供大家访问, 这样就能降低网页访问压力了, 当然了, 还有更多的场景, 需要结合实际分析, 大家一起来发掘吧~ see you again~
来源: http://blog.csdn.net/woshizisezise/article/details/79267176