1、Freemark最佳实践总结
在使用Freemark时对于一些页面不经常改变的内容,如页面的导航栏一般不经常改变。这些内容可以利用Freemark生成静态页面,以便于减少对于数据库的访问。由于减少了对于数据库的访问,也提高页面的加载速度。数据库的数据不经常改变,不代表不改变。可以利用AOP,当数据库的数据改变时,重新生成新的页面数据。
2、用于生成静态页面的FreemarkerUtil.java
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreemarkerUtil {
private static FreemarkerUtil util;
private static Configuration cfg;
private FreemarkerUtil() {
}
@SuppressWarnings("deprecation")
/**
* 获取FreemarkerUtil的单例
* @param pname freemark中的模板文件,存放的路径
* @return
*/
public static FreemarkerUtil getInstance(String pname) {
if (util == null) {
cfg = new Configuration();
cfg.setClassForTemplateLoading(FreemarkerUtil.class, pname);
util = new FreemarkerUtil();
}
return util;
}
private Template getTemplate(String fname) {
try {
return cfg.getTemplate(fname);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 通过标准输出流输出模板的结果
*
* @param root
* 数据对象
* @param fname
* 模板文件的名称
*/
public void sprint(Map<String, Object> root, String fname) {
try {
getTemplate(fname).process(root, new PrintWriter(System.out));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 基于文件的输出
*
* @param root
* 要在freemark里面,显示的数据
* @param fname
* 生成的文件名称
* @param outpath
* 生成文件的输出路径
*/
public void fprint(Map<String, Object> root, String fname, String outpath) {
try {
getTemplate(fname).process(root, new FileWriter(outpath));
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、因为生成静态页面,需要获取Web应用的文件路径,以便于保存生成的静态页面。在web.xml进行下面的配置
<!--用于获取,项目的根目录 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>baobaotao.root</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
在项目中使用,下面的代码。就可以获取到web应用的位置。
String realPath = System.getProperty("baobaotao.root");
4、关于使用Freemark生成静态页面的相关配置
4.1 接口的定义
public interface IIndexService {
/**
* 用于生成页面的,顶部的静态导航信息
*/
public abstract void generateTop(String realPath);
}
4.2 接口的实现
4.3 Freemark模板路径和生成的静态文件输出路径的配置
5、使用freemark提前生成,导航相关的静态页面
public class TestA {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:applicationContext-*.xml");
IIndexService indexService = context.getBean("indexService",
IIndexService.class);
String realPath = "D:\\MyEclipse 10\\wockplace 1\\FreemarkWeb\\WebRoot";
indexService.generateTop(realPath);
}
}
6、在index.jsp中,引入freemark生成的静态页面
7、关于AOP的配置
7.1 Aop的实现
public class TopAop {
private IIndexService indexService;
public IIndexService getIndexService() {
return indexService;
}
public void setIndexService(IIndexService indexService) {
this.indexService = indexService;
}
/**
* 用于生成页面的top部分
*/
public void generatorTop() {
String realPath = System.getProperty("baobaotao.root");
indexService.generateTop(realPath);
}
}
7.2 Aop的配置(applicationContext-freemarkaop.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!--配置freemark相关的Aop -->
<!--Aspect -->
<bean class="com.freemarkaop.TopAop" id="topAop">
<property name="indexService" ref="indexService" />
</bean>
<!--Aop配置 -->
<aop:config>
<aop:aspect ref="topAop">
<aop:pointcut
expression="execution(* com.service.NavService.add*(..)) ||
execution(* com.service.NavService.update*(..)) ||
execution(* com.service.NavService.delete*(..)) "
id="topPoint" />
<aop:after method="generatorTop" pointcut-ref="topPoint" />
</aop:aspect>
</aop:config>
</beans>
8、源码下砸