上一篇文章中我们讲了关于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~