Text template parsing
<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/");//////// /////////////////Get the path with the read file
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);
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326485544&siteId=291194637
Recommended
Ranking