[Java] java freemarker →→→→→进入此内容的聊天室

来自 , 2020-01-20, 写在 Java, 查看 125 次.
URL http://www.code666.cn/view/2bcab9d9
  1. import java.io.BufferedWriter;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. import java.io.StringWriter;
  8. import java.io.Writer;
  9. import java.util.Map;
  10.  
  11. import com.viewlinecn.consts.WebConst;
  12.  
  13. import freemarker.template.Configuration;
  14. import freemarker.template.DefaultObjectWrapper;
  15. import freemarker.template.Template;
  16. import freemarker.template.TemplateException;
  17. import freemarker.template.TemplateExceptionHandler;
  18.  
  19. /**
  20.  * FreeMarker操作类
  21.  *
  22.  * @author sunjun
  23.  *
  24.  */
  25. public class FreeMarkerUtil {
  26.  
  27.  /**
  28.   * 定义FreeMarker Configuration对象config
  29.   */
  30.  private Configuration config = null;
  31.  
  32.  /**
  33.   * 编码
  34.   */
  35.  private String encoding = "gbk";
  36.  
  37.  /**
  38.   * FreeMarkerUtil对象FREEMARKER
  39.   */
  40.  private static final FreeMarkerUtil FREEMARKER = new FreeMarkerUtil();
  41.  
  42.  /**
  43.   * 构造方法
  44.   *
  45.   * @throws IOException
  46.   */
  47.  private FreeMarkerUtil() {
  48.   try {
  49.    config = new Configuration();
  50.    config.setDirectoryForTemplateLoading(new File(
  51.      WebConst.APPLICATION_REAL_PATH));
  52.    // config.setTemplateLoader(getTemplateLoader(servletContext));
  53.    config
  54.      .setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
  55.    config.setDefaultEncoding(encoding);
  56.    config.setObjectWrapper(new DefaultObjectWrapper());
  57.   } catch (IOException e) {
  58.    e.printStackTrace();
  59.   }
  60.  }
  61.  
  62.  /**
  63.   * 得到FreeMarkerUtil对象
  64.   *
  65.   * @return FreeMarkerUtil对象
  66.   */
  67.  public static FreeMarkerUtil getInstance() {
  68.   return FREEMARKER;
  69.  }
  70.  
  71.  /**
  72.   * @param config
  73.   *            the config to set
  74.   */
  75.  public void setConfig(Configuration config) {
  76.   this.config = config;
  77.  }
  78.  
  79.  /**
  80.   * 设置编码
  81.   *
  82.   * @param encoding
  83.   *            the encoding to set
  84.   */
  85.  public void setEncoding(String encoding) {
  86.   this.encoding = encoding;
  87.  }
  88.  
  89.  /**
  90.   * 生成静态页面
  91.   *
  92.   * @param ftlFile
  93.   *            模板文件(相对根目录的绝对路径,以/开头)
  94.   * @param data
  95.   *            数据Map
  96.   * @param file
  97.   *            生成的文件
  98.   * @throws IOException
  99.   * @throws TemplateException
  100.   */
  101.  public void createHTML(String ftlFile, Map data, File file)
  102.    throws IOException, TemplateException {
  103.   Writer out = null;
  104.   FileOutputStream output = null;
  105.   try {
  106.    Template template = config.getTemplate(ftlFile);
  107.    output = new FileOutputStream(file);
  108.    out = new OutputStreamWriter(output, encoding);
  109.    template.process(data, out);
  110.   } catch (Exception ex) {
  111.    ex.printStackTrace();
  112.   } finally {
  113.    // 关闭out output
  114.    if (out != null) {
  115.     out.flush();
  116.     out.close();
  117.    }
  118.    if (output != null) {
  119.     output.flush();
  120.     output.close();
  121.    }
  122.   }
  123.  }
  124.  
  125.  /**
  126.   * 输出到字符串
  127.   *
  128.   * @param ftlFile
  129.   *            模板文件(相对根目录的绝对路径,以/开头)
  130.   * @param data
  131.   *            数据Map
  132.   * @return 结果字符串
  133.   */
  134.  public String getString(String ftlFile, Map data) {
  135.   StringWriter writer = null;
  136.   String result = "";
  137.   try {
  138.    Template template = config.getTemplate(ftlFile);
  139.    template.setEncoding(encoding);
  140.    writer = new StringWriter();
  141.    template.process(data, writer);
  142.    result = writer.toString();
  143.   } catch (Exception e) {
  144.    e.printStackTrace();
  145.   } finally {
  146.    // 关闭write
  147.    try {
  148.     if (writer != null) {
  149.      writer.flush();
  150.      writer.close();
  151.     }
  152.    } catch (Exception ex) {
  153.     ex.printStackTrace();
  154.    }
  155.   }
  156.   return result;
  157.  }
  158.  
  159.  /**
  160.   * 输出到内存
  161.   *
  162.   * @param ftlFile
  163.   *            模板文件(相对模板文件目录的相对路径)
  164.   * @param data
  165.   *            数据Map
  166.   * @return ByteArrayOutputStream
  167.   * @throws IOException
  168.   */
  169.  public ByteArrayOutputStream createByteArray(String ftlFile, Map data)
  170.    throws IOException {
  171.   ByteArrayOutputStream os = null;
  172.   BufferedWriter writer = null;
  173.   try {
  174.    Template template = config.getTemplate(ftlFile);
  175.    template.setEncoding(encoding);
  176.    os = new ByteArrayOutputStream();
  177.    writer = new BufferedWriter(new OutputStreamWriter(os));
  178.    template.process(data, writer);
  179.   } catch (Exception e) {
  180.    e.printStackTrace();
  181.   } finally {
  182.    // 关闭os write
  183.    if (writer != null) {
  184.     writer.flush();
  185.     writer.close();
  186.    }
  187.    if (os != null) {
  188.     os.flush();
  189.     os.close();
  190.    }
  191.   }
  192.   return os;
  193.  }
  194. }
  195. //该片段来自于http://yuncode.net
  196.  

回复 "java freemarker"

这儿你可以回复上面这条便签

captcha