Freemark发布引擎的简单运用

 

本例简单演示了利用freemark发布引擎动态提取数据库中的数据,然后发布到前端页面的功能。

 

第一步:准备好自己的发布模板

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="
http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>user.ftl</title>  
</head>  
  <body>  
    ${content.userName}   
    ${content.userPassword}   
   
   <#list userlist as user>
      <br>
     <a href="#"> ${user.userName}</a>
   </#list>
  </body>  
</html>

 

第二步:简单构建数据发布引擎工具包

package com.softeem.genhtml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkertUtil {
 
 /**
  *
  * @param templatePath 模板所在的目录
  * @param templateName 模板文件名
  * @param fileName 生成的静态文件名称
  * @param root 数据源
  */
 public static void analysisTemplate(String templatePath,   
            String templateName, String fileName, Map<?, ?> root) {   
        try {   
            //初使化FreeMarker配置   
            Configuration config = new Configuration();   
            // 设置要解析的模板所在的目录,并加载模板文件   
            config.setDirectoryForTemplateLoading(new File(templatePath));   
            // 设置包装器,并将对象包装为数据模型   
            config.setObjectWrapper(new DefaultObjectWrapper());   
  
            // 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致   
            // 否则会出现乱码   
            Template template = config.getTemplate(templateName, "UTF-8");   
            // 合并数据模型与模板   
            FileOutputStream fos = new FileOutputStream(fileName);   
            Writer out = new OutputStreamWriter(fos, "UTF-8");   
            template.process(root, out);   
            out.flush();   
            out.close();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } catch (TemplateException e) {   
            e.printStackTrace();   
        }  
 }


}

 

第三步:构建定时器

package com.softeem.task;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import com.softeem.dto.User;
import com.softeem.util.FreeMarkertUtil;

public class PubTask extends TimerTask{
 
 private String templatePath;
 
 private String templateName;
 
 private String fileName;
 
 public PubTask() {
  // TODO Auto-generated constructor stub
 }
 
 public PubTask(String templatePath,String templateName,String fileName) {
  // TODO Auto-generated constructor stub
  this.templatePath=templatePath;
  this.templateName=templateName;
  this.fileName=fileName;
 }
 
 synchronized public void doSomething(){
  //注意:此部分的数据在项目中来自于数据库
  FreeMarkertUtil fmu=new FreeMarkertUtil();
  //模板文件所在的目录
//  String templatePath="c:/template";
//  String templateName="/index.ftl";
//  String fileName=templatePath+"/index.html";
  //数据源对象
  Map<String, Object> root=new HashMap<String, Object>();
  
  List<User> userList=new ArrayList<User>();
  User user=new User();
  user.setUserName("席格");
  userList.add(user);
  root.put("userList", userList);
  
  Date dt=new Date();
  DateFormat formate=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  root.put("pubtm", formate.format(dt));
  
  
  fmu.analysisTemplate(templatePath, templateName, fileName, root);
  
  System.out.println("Hello world");
 }
 
 @Override
 public void run() {
  // TODO Auto-generated method stub
  doSomething();
 }
 
}

 

第四步:由于定时器需要和web服务器同时启动,所以加入监听器

package com.softeem.listener;

import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.softeem.task.PubTask;

/**
 *
 * @author yxx
 *
 */
public class PubListener implements ServletContextListener{
 
 
 public void contextInitialized(ServletContextEvent arg0) {
  // TODO Auto-generated method stub
  
  String path=arg0.getServletContext().getRealPath("template");
  String htmlFile=arg0.getServletContext().getRealPath("")+"/index.html";
        PubTask pt=new PubTask(path,"index.ftl",htmlFile);
  Timer tm=new Timer();
  tm.schedule(pt, new Date(),1000*10);
 }
 
 public void contextDestroyed(ServletContextEvent arg0) {
  // TODO Auto-generated method stub
  
 }

}

 

完成上述操作后,只需要在配置文件中配置监听,那么系统就会根据你的需要定时按照模板规则生成静态网页了,如果需要做更复杂的发布,可以在此基础上做扩展。欢迎和各位朋友一起交流静态发布技术。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值