文字模板解析

来源:互联网 发布:java哪里下载 编辑:程序博客网 时间:2024/04/30 12:32

   freemark文字模板解析:

   1,导入freeMark的jar包

   <dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>

   2,建立模板文件:
   .tpl
   ${CUSTOMER_NAME!!}您好:
<br>您已成功注册成为点钢网个人会员!您的登录账号是${CUSTOMER_ID!!}。
<br>
<br>
<br>您的注册信息如下,请你仔细核对,如有错误,请登录您的账户进行个人信息修改;如果有任何问题,请拨打我们的
3,读取tpl文件
  public static String getFileContent(String strPath, String encoding)
  {
  WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); 
    ServletContext servletContext = webApplicationContext.getServletContext();
   String p=  servletContext.getRealPath("/WEB-INF/");//////////////////////////获取带读取文件的路劲
    String strContent = "";
   
    createFile(p+strPath);
    BufferedReader br = null;
    try
    {
      br = new BufferedReader(new InputStreamReader(new FileInputStream(p+strPath), "utf-8"));
      String strTemp = "";
      int i = 0;
      do
      {
        strTemp = br.readLine();
        if (strTemp != null) {
          if (i == 0)
          {
            strContent = strContent + strTemp;
            i++;
          }
          else
          {
            strContent = strContent + '\n' + strTemp;
          }
        }
      } while (strTemp != null);
      br.close();
      br = null;
     
      return strContent;
    }
    catch (Exception e)
    {
      strContent = "";
    }
    finally
    {
      try
      {
        if (br != null)
        {
          br.close();
          br = null;
        }
      }
      catch (Exception e)
      {
      }
    }
return strContent;
  }
4,用freemark填充并获取字符串;

public static String parseData(String tplContent, Map<String, Object> datas)
  {
    if (StringUtils.isNull(tplContent)) {
      return "";
    }
    if (datas == null) {
      return tplContent;
    }
    StringWriter  writer = new StringWriter();
    Configuration cfg = new Configuration();
    StringTemplateLoader stringLoader = new StringTemplateLoader();
    stringLoader.putTemplate("myTemplate",tplContent); 
    cfg.setTemplateLoader(stringLoader);
    cfg.setEncoding(Locale.getDefault(), "UTF-8");
    Writer out = null;
    try
    {
      datas.put("statics", BeansWrapper.getDefaultInstance().getStaticModels());
      Template template = cfg.getTemplate("myTemplate");
      template.setEncoding("UTF-8");
      template.process(datas, writer);
      tplContent=writer.toString();
      return writer.toString();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      try
      {
        if (out != null)
        {
          out.flush();
          out.close();
        }
      }
      catch (Exception ee)
      {
        ee.printStackTrace();
      }
    }
return tplContent;
  }

应用部分:
content=FreemarkerUtils.parseData(FileUtil.getFileContent("/tpl/regAudit_C.tpl", "UTF-8"), param);