使用freemarker實現網頁靜態化
這是Freemarker需要的模板
1.1. 什麼是freemarker
FreeMarker是一個用Java語言編寫的模板引擎,它基於模板來生成文本輸出。FreeMarker與Web容器無關,即在Web運行時,它並不知道Servlet或HTTP。它不僅可以用作表現層的實現技術,而且還可以用於生成XML,JSP或Java 等。
目前企業中:主要用Freemarker做靜態頁面或是頁面展示
1.2. Freemarker的使用方法
把freemarker的jar包添加到工程中。
Maven工程添加依賴
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
|
原理
使用步驟:
第一步:創建一個Configuration對象,直接new一個對象。構造方法的參數就是freemarker對於的版本號。
第二步:設置模板文件所在的路徑。
第三步:設置模板文件使用的字符集。一般就是utf-8.
第四步:加載一個模板,創建一個模板對象。
第五步:創建一個模板使用的數據集,可以是pojo也可以是map。一般是Map。
第六步:創建一個Writer對象,一般創建一FileWriter對象,指定生成的文件名。
第七步:調用模板對象的process方法輸出文件。
第八步:關閉流。
示例
模板:
${hello}
@Test publicvoid genFile() throws Exception { // 第一步:創建一個Configuration對象,直接new一個對象。構造方法的參數就是freemarker對於的版本號。 Configuration configuration = new Configuration(Configuration.getVersion()); // 第二步:設置模板文件所在的路徑。 configuration.setDirectoryForTemplateLoading(new File("D:/workspaces-itcast/term197/e3-item-web/src/main/webapp/WEB-INF/ftl")); // 第三步:設置模板文件使用的字符集。一般就是utf-8. configuration.setDefaultEncoding("utf-8"); // 第四步:加載一個模板,創建一個模板對象。 Template template = configuration.getTemplate("hello.ftl"); // 第五步:創建一個模板使用的數據集,可以是pojo也可以是map。一般是Map。 Map dataModel = new HashMap<>(); //向數據集中添加數據 dataModel.put("hello", "this is my first freemarker test."); // 第六步:創建一個Writer對象,一般創建一FileWriter對象,指定生成的文件名。 Writer out = new FileWriter(new File("D:/temp/term197/out/hello.html")); // 第七步:調用模板對象的process方法輸出文件。 template.process(dataModel, out); // 第八步:關閉流。 out.close(); } |
宜立方商城中運用
ActiveMq同步生成靜態網頁
1.1. 商品詳情頁面靜態化
1.1.1. 網頁的靜態化方案
輸出文件的名稱:商品id+“.html”
輸出文件的路徑:工程外部的任意目錄。
網頁訪問:使用nginx訪問網頁。在此方案下tomcat只有一個作用就是生成靜態頁面。
工程部署:可以把e3-item-web部署到多個服務器上。
生成靜態頁面的時機:商品添加後,生成靜態頁面。可以使用Activemq,訂閱topic(商品添加)(這就是我們需要的消息來源發送消息方)
這裏我們的服務層需要一個發送來的消息
//發送一個商品添加信息 jmsTemplate.send(topicDestination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { //設置消息內容爲商品id值需要轉換一下類型爲字符串 TextMessage textMessage = session.createTextMessage(itemId+""); return textMessage; } });
項目表現層模塊劃分結構
Item.java代碼
/** * 2、根據商品id查詢商品信息(tb_item)得到一個TbItem對象,缺少images屬性, * 可以創建一個pojo繼承TbItem,添加一個getImages方法。在e3-item-web工程中。 */ public class Item extends TbItem { /** * 初始化所有值 * @param tbItem */ public Item(TbItem tbItem){ this.setId(tbItem.getId()); this.setTitle(tbItem.getTitle()); this.setSellPoint(tbItem.getSellPoint()); this.setPrice(tbItem.getPrice()); this.setNum(tbItem.getNum()); this.setBarcode(tbItem.getBarcode()); this.setImage(tbItem.getImage()); this.setCid(tbItem.getCid()); this.setStatus(tbItem.getStatus()); this.setCreated(tbItem.getCreated()); this.setUpdated(tbItem.getUpdated()); } /** * 返回字符串images * @return */ public String[] getImages(){ String image2 = this.getImage(); if (image2!=null && !"".equals(image2)){ String[] strings = image2.split(","); return strings; } return null; }
HtmlGenListtener.java代碼
resource.properties
/** * 監聽商品添加信息,生成對應的靜態頁面 * @Auther: Administrator * @Date: 2018/5/30 0030 17:06 * @Description: */ public class HtmlGenListener implements MessageListener { @Autowired private ItemService itemService; @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @Value("${HTML_GEN_PATH}") private String HTML_GEN_PATH; @Override public void onMessage(Message message) { try { //從消息中取出商品id TextMessage textMessage= (TextMessage) message; String text = textMessage.getText(); Long itemId= Long.valueOf(text); System.out.println(itemId); //等待事務提交 Thread.sleep(1000); //根據商品id查詢商品信息 TbItem tbItem = itemService.getItemById(itemId); Item item=new Item(tbItem); //根據商品id查詢商品描述信息 TbItemDesc tbItemDesc = itemService.selectTbItemDesc(itemId); //創建一個數據集,把商品數據封裝 Map data=new HashMap(); data.put("item",item); data.put("itemDesc",tbItemDesc); //加載模板對象 Configuration configuration = freeMarkerConfigurer.getConfiguration(); Template template = configuration.getTemplate("item.ftl"); //創建一個輸出流,指定輸出的目錄和文件名 Writer out = new FileWriter(HTML_GEN_PATH + itemId + ".html"); // 使用模板生成靜態頁面 template.process(data,out); //關閉流 out.close(); } catch (Exception e) { e.printStackTrace(); } }
applicationContent-activemq.xml配置文件寫入
<!-- 真正可以產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.128:61616" /> </bean> <!-- Spring用於管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目標ConnectionFactory對應真實的可以產生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /> </bean> <!--這個是主題目的地,一對多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="itemAddTopic" /> </bean> <!-- 監聽商品添加消息,同步索引庫 --> <bean id="htmlGenListener" class="com.e3mall.item.listener.HtmlGenListener"/> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicDestination" /> <property name="messageListener" ref="htmlGenListener" /> </bean>
springmvc.xml配置freemarker
<!--配置Freemarker--> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <property name="defaultEncoding" value="UTF-8" /> </bean>
這是Freemarker生成的html頁面存放位置
我們這裏配置一些Windows 7 下Nginx
解壓Nginx文件
在conf文件中找到nginx.conf文件打開配置紅色的端口號和存放的Freemarker文件的路徑
成功頁面