freemaker生成代码

来源:互联网 发布:移动软件开发定义 编辑:程序博客网 时间:2024/06/17 05:12
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