e3mall项目:前台商品详情展示(生成静态页面)

e3mall项目:前台商品详情展示

分析:

1、输出文件的名称:商品id+“.html”

2、输出文件的路径:工程外部的任意目录。

3、网页访问:使用nginx访问网页。在此方案下tomcat只有一个作用就是生成静态页面。

4、工程部署:可以把e3-item-web部署到多个服务器上。

5、生成静态页面的时机:商品添加后,生成静态页面。可以使用Activemq,订阅topic(商品添加)


准备工作:在e3-item-web工程中,导入activemq以及freemarker依赖。准备freemarker的模板文件(自行准备)

        <!--ActiveMQ消息中间件-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
        </dependency>
        <!--freemarker-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
        </dependency>



一、在配置文件中,配置ActiveMQ,配置Freemarker整合

(1)ActiveMQ配置(applicationContext-activemq.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">


    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="${activemq.url}" />
    </bean>
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
    <bean id="connectionFactory"
          class="org.springframework.jms.connection.SingleConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
        <property name="targetConnectionFactory" ref="targetConnectionFactory" />
    </bean>

    <!-- 配置生产者 -->
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>
    <!-- 商品数据更新消息 -->
    <bean id="itemUpdate" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="itemUpdate" />
    </bean>

    <!--配置消息接收监听器-->
    <bean id="htmlGenMessageListener" class="cn.e3mall.item.message.HtmlGenMessageListener"/>
    <!--商品数据更新消息监听器容器-->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="itemUpdate" />
        <property name="messageListener" ref="htmlGenMessageListener" />
    </bean>

</beans>

(2)配置整合freemarker(在springmvc.xml新增配置)

    <!--整合freemarker-->
    <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

(3)resource.properties配置

## ActiveMQ 服务URL
activemq.url=tcp://192.168.25.128:61616

## freemarker生成的静态资源存放位置
freemarker.filepath=E:/temp/freemarker/item/


二、书写商品添加消息监听器(HtmlGenMessageListener)

package cn.e3mall.item.message;

import cn.e3mall.entity.TbItem;
import cn.e3mall.entity.TbItemDesc;
import cn.e3mall.item.entity.Item;
import cn.e3mall.service.ItemService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
 * 生成商品详情静态页面
 * Author: xushuai
 * Date: 2018/5/28
 * Time: 12:41
 * Description:
 */
public class HtmlGenMessageListener implements MessageListener {

    @Autowired
    private ItemService itemService;

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${freemarker.filepath}")
    private String filepath;

    @Override
    public void onMessage(Message message) {
        try {
            //等待添加商品端事务提交
            Thread.sleep(1000);
            //获取新增商品ID
            TextMessage textMessage = (TextMessage) message;
            Long itemId = Long.valueOf(textMessage.getText());
            //根据itemId 查询商品信息
            TbItem tbItem = (TbItem) itemService.load(itemId).getData();
            //将查询到的数据转换成需要的数据格式
            Item item = new Item(tbItem);
            //根据itemId 查询商品描述信息
            TbItemDesc desc = (TbItemDesc) itemService.findDescByItemid(itemId).getData();
            //将数据封装
            Map map = new HashMap();
            map.put("item",item);
            map.put("itemDesc",desc);

            //加载freemarker配置文件
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            Template template = configuration.getTemplate("item.ftl");
            //创建输出流
            Writer writer = new FileWriter(filepath + itemId + ".html");
            //输出
            template.process(map,writer);
            //关闭资源
            writer.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

三、配置本地nginx测试环境

(1)静态资源保存的位置(与你的配置文件中的位置对应)


(2)配置nginx配置文件


(3)将所需样式文件,放入nginx指定的root目录下



四、测试:添加商品,生成静态页面


猜你喜欢

转载自blog.csdn.net/qq1031893936/article/details/80481073
今日推荐