博客
关于我
商城项目中使用Freemarker生成静态页面html
阅读量:346 次
发布时间:2019-03-04

本文共 2360 字,大约阅读时间需要 7 分钟。

在商城项目中,通过技术手段实现静态页面生成,可以有效提升网站性能和用户体验。本文将介绍如何利用Freemarker模板引擎和消息队列技术,实现商品上架后的静态页面生成机制。

一、静态模板制作

原有的产品详情页面通常是JSP形式,改写为静态HTML模板后,需要注意以下几点:

  • 模板结构优化:去除不必要的C标签,将动态标签替换为Freemarker模板标签。例如,列表循环可以用<#list persons as person>实现。
  • 模板引入:通过<#include>标签引入公共模块,例如导入 footer.html。
  • 字符编码设置:在模板顶部添加<meta charset="UTF-8">,确保页面字符集一致。
  • 二、配置Freemarker模板引擎

    在项目中添加必要的Freemarker配置文件freemarker.xml:

    三、消息队列监听

    为了实现动态静态页面生成,需要设置消息队列监听:

  • JMS配置:
  • public class CustomMessageListener implements MessageListener {    @Autowired    private StaticPageService staticPageService;        @Override    public void onMessage(Message message) {        try {            String productId = ((ActiveMQTextMessage) message).getText();            // 查询商品详情            Product product = cmsService.selectProductById(productId);            // 查询库存信息            List
    skus = cmsService.selectSkuListByProductId(productId); Set
    colors = new HashSet<>(); for (Sku sku : skus) { colors.add(sku.getColor()); } Map
    root = new HashMap<>(); root.put("product", product); root.put("skus", skus); root.put("colors", colors); staticPageService.productStaticPage(root, productId); } catch (JMSException e) { e.printStackTrace(); } }}
    1. 消息监听配置:
    2. 四、生成静态页面

      静态页面生成的具体实现逻辑:

      public void productStaticPage(Map
      root, String id) { String path = getPath("/html/product/" + id + ".html"); File f = new File(path); File parentFile = f.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } try { Template template = conf.getTemplate("productDetail.html"); Writer out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8"); template.process(root, out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != out) { out.close(); } } catch (IOException e) { e.printStackTrace(); } }}

      五、链接优化

      在产品列表页面,修改商品链接:

          ...

      六、项目验证

      完成上述配置后,通过浏览器访问商城页面,验证静态页面生成效果。

      通过以上步骤,可以实现商品上架后自动生成静态HTML页面,提升网站性能表现。

    转载地址:http://qnse.baihongyu.com/

    你可能感兴趣的文章
    PYTHON--欧拉习题二
    查看>>
    python--高级应用总结
    查看>>
    python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
    查看>>
    Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
    查看>>
    Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
    查看>>
    Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
    查看>>
    python出现 unknown encoding: idna 的处理方法
    查看>>
    Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
    查看>>
    Python-numpy(2)数组的操作
    查看>>
    Python-Numpy的tile函数用法
    查看>>
    python-open函数
    查看>>
    Python-Pandas-将特定函数应用于给定级别-多索引数据框架
    查看>>
    python-queue队列通信
    查看>>
    python-ros No module named PyKDL
    查看>>
    python-social-auth 是否需要会话
    查看>>