freemaker生成代码

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.output.FileWriterWithEncoding;

import com.frame.system.FrameProperty;
import com.frame.system.entity.ZealotEntityMapping;

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

public class CreateJspUtil {
    public static void createJspList(String fileName,String codePath,List<ZealotEntityMapping> list) {  
        Configuration cfg = new Configuration();  
        String[] nameArr=list.get(0).getEntityclass().split("\\.");
        String name=nameArr[nameArr.length-1];
        try {  
            // 从哪里加载模板文件  
            cfg.setDirectoryForTemplateLoading(new File(FrameProperty.APP_PATH+File.separator+"WebRoot"+File.separator+FrameProperty.JSP_MODEL_URL));
              
            // 定义模版的位置,从类路径中,相对于FreemarkerManager所在的路径加载模版  
            // cfg.setTemplateLoader(new ClassTemplateLoader(FreemarkerManager.class, "templates"))  
  
            // 设置对象包装器  
            cfg.setObjectWrapper(new DefaultObjectWrapper());  
  
            // 设置异常处理器  
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);  
            cfg.setDefaultEncoding("UTF-8");
            
            StringBuffer thContent=new StringBuffer();
            StringBuffer tdContent=new StringBuffer();
            for(ZealotEntityMapping entity :list){
            	thContent.append("<th>"+entity.getFieldlabel()+"</th>");
            	tdContent.append("<td>${entity."+entity.getFieldname()+"}</td>");
            }
            // 定义数据模型  
            Map root = new HashMap();  
            root.put("modelName", fileName);  
            root.put("thContent", thContent); 
            root.put("tdContent", tdContent); 
            root.put("list", "${list}");
            root.put("ctx", "${ctx}");
  
            // 通过freemarker解释模板,首先需要获得Template对象  
            Template template = cfg.getTemplate("list.ftl");  
            template.setEncoding("utf-8");
            
            
            
            File output_list = new File(FrameProperty.APP_PATH+File.separator+"WebRoot"+File.separator+codePath+File.separator+name+"-list.jsp");
            Writer writer_list = new FileWriterWithEncoding(output_list,"UTF-8");  

            // 定义模板解释完成之后的输出  
            /*PrintWriter out = new PrintWriter(new BufferedWriter(  
                    new FileWriter(FrameProperty.APP_PATH+File.separator+"WebRoot"+File.separator+codePath+File.separator+name+"-list.jsp")));  */
            try {  
                // 解释模板  
                template.process(root, writer_list);  
            } catch (TemplateException e) {  
                e.printStackTrace();  
            }  
  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值