部分Web项目需要将页面静态化,JFinal增加几行代码即可搞定。
例如:
假定原有Controller Action中首页方法如下/**
* 首页
*/
public void index() {
//原模版路径
String yourTplPath = "index.html";
render(yourTplPath);
}
静态化只需要增加几行代码即可,
经过波总的指导,有更方便的写法。/**
* 首页
*/
public void index() {
//原模版路径
String yourTplPath = "index.html";
//静态html文件保存的路径
String staticHtmlPath = "/jfinal.com/index.html";
Template template = RenderManager.me().getEngine().getTemplate(yourTplPath);
template.render(null, outPath);//直接输出,简洁
render(yourTplPath);//是否继续输出到浏览器按自己的业务需求决定
}
以上只是示例,给有需要的朋友。
另外在静态化的过程中,有个小坑,需要注意下:
部分应用可能会存在部署目录,即常用的ctx,Jfinal中配置如下:public void configHandler(Handlers me) {
me.add(new ContextPathHandler("base"));
}
原模版中使用 #(base) 输出没问题,即为“”,但在生成静态化的文件时,直接输出了 null,兼容的写法为 #(base??"")
EOF