当前在线人工客服
天达云-技术
QQ:1324027256
天达云-技术
QQ:1902643386
天达云-售前
QQ:1650874901
天达云-售前
QQ:2207995833
天达云-合作
QQ:1984186903
天达云微信小程序
400-837-6568

web开发中页面静态化的示例分析
更新:HHH   时间:2023-1-7


这篇文章给大家分享的是有关web开发中页面静态化的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

  public void GenerathHtmlByString() throws IOException, TemplateException {

  //创建配置类

  Configuration configuration=new Configuration(Configuration.getVersion());

  //模版内容

  String templateString="<html>\n" +

  "<head>\n" +

  "    <meta charset="UTF-8">\n" +

  "    <title>FreemarkerTest1</title>\n" +

  "</head>\n" +

  "<body>\n" +

  "   老同志,你也不要拽,${song}!\n" +

  "   \n" +

  "</body>\n" +

  "</html>";

  //模版加载器

  StringTemplateLoader loader=new StringTemplateLoader();

  loader.putTemplate("template", templateString);

  configuration.setTemplateLoader(loader);

  //得到模版

  Template template=configuration.getTemplate("template", "UTF-8");

  ?

  //定义数据模型

  Map<String,Object> map=new HashMap<>();

  map.put("song", "你一个月的工资才二十来块");

  //静态化

  String content= FreeMarkerTemplateUtils.processTemplateIntoString(template, map);

  System.out.println("我们打印一下静态化的内容 :" + content);

  ?

  //转化为流

  InputStream inputStream= IOUtils.toInputStream(content);

  //输出文件

  FileOutputStream fileOutputStream=new FileOutputStream(new File("F:\\test2.html"));

  int copy= IOUtils.copy(inputStream, fileOutputStream);

  System.out.println(copy);//209

  }

感谢各位的阅读!关于“web开发中页面静态化的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

返回web开发教程...