博客
关于我
商城项目中使用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 实现儿童算术游戏
    查看>>
    python 实现字符串转整型
    查看>>
    Python 实现定时任务的八种方案!
    查看>>
    Python 实现自动化测试 dubbo 协议接口
    查看>>
    python 对json数据读取及保存与读取,对dump,dumps,load,loads的理解
    查看>>
    Python 对mysql数据库的操作
    查看>>
    Python 对象数据库列表
    查看>>
    Python 导入requests报错No module named requests
    查看>>
    python 将函数加到列表中进行调用
    查看>>
    python 将图片与字符串相互转换
    查看>>
    Python 嵌套字典全面指南
    查看>>
    Python 常见的错误类型和继承关系
    查看>>
    python 常量_零基础学Python系列之一:Python的变量与常量
    查看>>
    Python 并发编程
    查看>>
    python 序列化操作
    查看>>
    Python 开发者,这 7 个 VS Code 插件极力推荐
    查看>>
    python手把手视频_硬货 | 手把手带你构建视频分类模型(附Python演练))
    查看>>
    python 录音左右声道_Python分离立体声wav压缩文件的左右声道
    查看>>