博客
关于我
商城项目中使用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 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
    查看>>
    Python 从数据库中存储和检索密码的最安全方法
    查看>>
    Python语言及其应用 - 知识点遍历
    查看>>
    Python 优化提速的 8 个小技巧
    查看>>
    Python 余弦相似度与皮尔逊相关系数 计算
    查看>>
    python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
    查看>>
    python 使用filetype校验文件
    查看>>
    Python 使用flush函数将缓冲区数据立即写磁盘
    查看>>
    python 使用in判断不准确,in不好使
    查看>>
    Python 使用pandas 进行查询和统计详解
    查看>>
    Redis 配置文件redis.conf详细解释
    查看>>
    python网络爬虫(2)——scrapy框架的基础使用
    查看>>
    python网络爬虫实例教程试读_Python网络爬虫实战教程(全套完整版) - 学途无忧网 - 做技术的王者 - Powered By EduSoho...
    查看>>
    Python 使用哈希函数用于加密
    查看>>
    Python 依赖管理的革新——Poetry 深度解析
    查看>>
    python 保留精度及增加去除数字的千位分隔符(金额化数字)
    查看>>
    python 倒计时 9,8,7,。。。。。。0
    查看>>
    Python 入门开发学习笔记之数据的增删改查
    查看>>
    Python 入门教程(2)搭建环境 2.4、VSCode配置Node.js运行环境
    查看>>