示例#1
0
  public static void main(String[] args) throws Exception {

    /* You should do this ONLY ONCE in the whole application life-cycle: */

    /* Create and adjust the configuration singleton */
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    cfg.setDirectoryForTemplateLoading(new File("/Users/ian.goldsmith/projects/freemarker"));
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    /*
     * You usually do these for MULTIPLE TIMES in the application
     * life-cycle:
     */

    /* Create a data-model */
    Map message = new HashMap();
    message.put(
        "contentAsXml",
        freemarker.ext.dom.NodeModel.parse(
            new File("/Users/ian.goldsmith/projects/freemarker/test.xml")));

    Map root = new HashMap();
    root.put("message", message);

    /* Get the template (uses cache internally) */
    Template temp = cfg.getTemplate("testxml.ftl");

    /* Merge data-model with template */
    Writer out = new OutputStreamWriter(System.out);
    temp.process(root, out);
    // Note: Depending on what `out` is, you may need to call `out.close()`.
    // This is usually the case for file output, but not for servlet output.
  }
  public void processTemplate(
      final Object obj, final String templateName, final OutputStream outputStream)
      throws IOException {
    final Template template = config.getTemplate(templateName);

    final Writer writer = new OutputStreamWriter(outputStream);
    try {
      template.process(obj, writer);
    } catch (final NullPointerException | TemplateException e) {
      LOG.error("Unable to process template " + templateName, e);
    }
  }
示例#3
0
 @Test
 public void freeMarkerTest() throws IOException, TemplateException {
   Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
   cfg.setDirectoryForTemplateLoading(new File("D:/template/"));
   cfg.setDefaultEncoding("UTF-8");
   Template template = cfg.getTemplate("test.xml");
   Writer writer = new FileWriter(new File("d:/test.xls"));
   Map<String, Object> data = new HashMap<>();
   data.put("area", "A区");
   data.put("code", "A001");
   data.put("row", 2);
   template.process(data, writer);
 }
示例#4
0
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   Map root = new HashMap();
   Connection conn = null;
   conn = ConFact.getInstance().makeConnection();
   String id = req.getParameter("id");
   String sql = "select title,description,language_id from film where film_id=" + id + ";";
   java.sql.PreparedStatement pst;
   try {
     pst = conn.prepareStatement(sql);
     ResultSet rs = pst.executeQuery();
     while (rs.next()) {
       java.sql.PreparedStatement pst1;
       String sql1 = "select name from language where language_id=" + rs.getString(3) + ";";
       pst1 = conn.prepareStatement(sql1);
       ResultSet rs1 = pst1.executeQuery();
       while (rs1.next()) {
         Template t = cfg.getTemplate("test.ftl");
         root.put("title", rs.getString(1));
         root.put("description", rs.getString(2));
         root.put("language", rs1.getString(1));
         resp.setContentType("text/html; charset=" + t.getEncoding());
         Writer out = resp.getWriter();
         t.process(root, out);
       }
       rs1.close();
     }
     rs.close();
     pst.close();
     conn.close();
   } catch (TemplateException e) {
     e.printStackTrace();
   } catch (SQLException e1) {
     e1.printStackTrace();
   }
 }
  /** @param args */
  public static void main(String[] args) {
    ModelParser p = new ModelParser();
    CIISModel m = p.parseModel(cInBase + "BOB_ECIF_逻辑模型模板.xls");
    Template template;
    File afile;
    Writer out;

    CIISObject[] objs = m.getAllObjects();
    Map params = new HashMap();
    params.put("today", new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    params.put("objects", objs);

    params.put("utils", new Utils());
    try {
      Configuration cfg = new Configuration();
      cfg.setLocalizedLookup(false);
      cfg.setDirectoryForTemplateLoading(new File(cInBase + "javaTemplate"));
      cfg.setObjectWrapper(new DefaultObjectWrapper());

      template = cfg.getTemplate("transBaseErrorCode.ftl");
      afile = new File(cOutJava + "ITransBaseErrorCode.java");
      out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
      template.process(params, out);
      out.flush();

      cfg = new Configuration();
      cfg.setLocalizedLookup(false);
      cfg.setDirectoryForTemplateLoading(new File(cInBase + "javaTemplate"));
      cfg.setObjectWrapper(new DefaultObjectWrapper());

      template = cfg.getTemplate("transBaseErrorCode_sql.ftl");
      afile = new File(configOutJava + "transBaseErrorCode.sql");
      out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
      template.process(params, out);
      out.flush();

      cfg = new Configuration();
      cfg.setLocalizedLookup(false);
      cfg.setDirectoryForTemplateLoading(new File(cInBase + "javaTemplate"));
      cfg.setObjectWrapper(new DefaultObjectWrapper());

      template = cfg.getTemplate("datamodelConst.ftl");
      afile = new File(cOutJava + "DataModelConst.java");
      out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
      template.process(params, out);
      out.flush();

      template = cfg.getTemplate("base.operation.ftl");
      afile = new File(configOutJava + "base.operation.xml");
      out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
      template.process(params, out);
      out.flush();

      template = cfg.getTemplate("errorcode.xml.ftl");
      afile = new File(configOutJava + "errorcode.xml");
      out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
      template.process(params, out);
      out.flush();

    } catch (TemplateException e) {
      System.out.println("模板出现错误!");
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }