• Freemarker生成HTML静态页面


    这段时间的工作是做一个网址导航的项目,面向用户的就是一个首页,于是就想到了使用freemarker这个模板引擎来对首页静态化。

      之前是用jsp实现,为了避免用户每次打开页面都查询一次数据库,所以使用了jsp的内置对象application,在Controller中将数据都查询出来,

    然后放入application,最后在JSP页面使用jstl标签配合EL表达式 将数据遍历出来。这样做是从一定程度上减轻了服务器的压力和页面的响应速度,

    但是仍然没有静态页面响应快。

      使用Freemarker步骤:

    1. jar包,我的项目中使用maven来构建,所以在pom.xml中引入Freemarker jar包的坐标就可以了。
    2. ftl模板,我在WEB-INF下面创建一个文件夹ftl,里面只放ftl模板文件,我创建了一个index.ftl文件。
    3. ftl模板文件中写的就是html标签和css样式之类的,但是数据部分需要使用Freemarker提供的标签遍历出来。如下
                              <!--广告悬浮-->
                              <div class="subMenu">
                                  <!--工具-->
                                  <div class='xff'>
                                      <div class="slideTxtBox">
                                          <div class="hd">
                                              <span class="arrow"><a class="next"></a><a class="prev"></a></span>
                                              <ul>
                                                  <#list newsMap?keys as testKey>
                                                      <li>${testKey}</li>
                                                  </#list>
                                              </ul>
                                          </div>
                                          <div class="bd" style="padding: 5px 10px;">
                                              <#list newsMap?values as value>
                                                  <div style="text-align: left; table-layout: fixed; word-wrap: break-word;  100%;" class="baidu">
                                                  <#list value as newsList>
                                                      <a target="_blank" href="${newsList.newsurl }" title="${newsList.newsname }">${newsList.newsname }</a>
                                                  </#list>
                                                  </div>
                                              </#list>
                                          </div>
                                      </div>
                                  </div>
                              </div>

      其中<#list></#list>是Freemarker提供的遍历标签,Freemarker提供了很多的标签,这里不一一叙述。

    4. Contorller中将数据都查询出来,通过ftl模板取出数据,最后将完整的数据写入html
          // 获取搜索引擎
              List<SearchEngines> searchEngines = this.indexService.findSearchEngines();
              // 获取热搜客户
              List<Catalog> hotSearchs = this.indexService.findHotSearchs();
              // 获取前25个一级目录
              CatalogCustom custom = new CatalogCustom();
              custom.setCatalogLevel(1);
              List<Catalog> topLevelCatalog = this.indexService.findCustomers(custom);
              // 获取一级目录下的前十个客户
              Map<String, List<Catalog>> customerMap = new HashMap<String, List<Catalog>>();
              
              for (Catalog catalog : topLevelCatalog) {
                  CatalogCustom customer = new CatalogCustom();
                  customer.setCatalogLevel(3);
                  customer.setGfid(catalog.getCatalogId());
                  
                  List<Catalog> customerList = this.indexService.findCustomers(customer);
                  
                      customerMap.put(catalog.getCatalogName(), customerList);
                  
                  
              }
              // 获取新闻相关数据
              Map<String, List<News>> newsMap = new HashMap<String, List<News>>();
              List<NewsCatalog> newsCatalogs = this.indexService.findNewsCatalog();
              
              for (NewsCatalog newsCatalog : newsCatalogs) {
                  
                  News news = new News();
                  news.setPid(newsCatalog.getId());
                  List<News> newsList = this.indexService.findNews(news);
                  
                  newsMap.put(newsCatalog.getNewscatalog(), newsList);
                  
              }
              // 获取关键词
              List<Keywords> keywords = this.indexService.findKeywords();
              /*
              application.setAttribute("newsMap", newsMap);
              application.setAttribute("searchEngines", searchEngines);
              application.setAttribute("hotSearchs", hotSearchs);
              application.setAttribute("customerMap", customerMap);
              application.setAttribute("keywords", keywords);
              */
              
              String ftlPath = session.getServletContext().getRealPath("/WEB-INF/ftl");
              
              Configuration configuration = new Configuration();
              configuration.setDirectoryForTemplateLoading(new File(ftlPath));
              configuration.setDefaultEncoding("UTF-8");
              // 获取或创建一个模版。  
              Template template = configuration.getTemplate("index.ftl");
              // 获取html静态页面文件
              String indexPath = session.getServletContext().getRealPath("/index.html");
              //设置文件输入流编码,不然生成的html文件会中文乱码
              FileWriterWithEncoding out = new FileWriterWithEncoding(indexPath,"UTF-8");
              // 将页面中要展示的数据放入一个map中
              HashMap<String,Object> map = new HashMap<String, Object>();
              map.put("newsMap", newsMap);
              map.put("searchEngines", searchEngines);
              map.put("hotSearchs", hotSearchs);
              map.put("customerMap", customerMap);
              map.put("keywords", keywords);
              //将map中的数据输入到index.ftl这个模板文件中并遍历出来,最后再将整个模板的数据写入到index.html中。
              template.process(map, out);
              out.close();
  • 相关阅读:
    Java练习题
    循环练习
    Java String类详解
    Collections类
    TreeSet类
    IOS基础:NSTimer类的使用
    IOS基础:深入理解Objectivec中@class的含义
    IOS基础:retain,copy,assign及autorelease
    IOS基础:ActionSheet(上拉菜单)的实现
    Xcode 模拟器生成目标目录位置查找
  • 原文地址:https://www.cnblogs.com/panchanggui/p/10405640.html
Copyright © 2020-2023  润新知