商城项目中使用Freemarker生成静态页面html

本博客介绍,指定条件下生成静态html页面,生成后,网站不再访问jsp页面,而是访问html.

举例:一个商场购物网站,商品上架时候,发送商品id到activeMQ,生成静态页面服务器监听activeMQ消息,并生成静态页面,后续多个服务器访问产品详情时候,不再需要进jsp,只需要跳到对应的html即可。

另freemark简单使用方法:导入freemarker-2.3.16.jar,见附件:http://download.csdn.net/download/w20228396/10262877

下面结合具体商城案例使用:

1、制作静态模板

对于原产品详情的jsp页面进行改写,去掉c标签,部分标签换为freemarker的标签,如:遍历用


   
   
  1. <#list persons as person>
  2. ${person_index} ${person} <br/>
  3. </#list>

引入其他页面,如上下左右等边角页面用

<#include "commons/footer.html" />
   
   

加入UTF-8

<meta charset="UTF-8">
   
   

2、编写freemarker.xml配置文件,放在生成静态页面的项目中


   
   
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc= "http://www.springframework.org/schema/mvc"
  3. xmlns:context= "http://www.springframework.org/schema/context"
  4. xmlns:aop= "http://www.springframework.org/schema/aop"
  5. xmlns:tx= "http://www.springframework.org/schema/tx"
  6. xmlns:task= "http://www.springframework.org/schema/task"
  7. xmlns:dubbo= "http://code.alibabatech.com/schema/dubbo"
  8. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  16. http://www.springframework.org/schema/tx
  17. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  18. http://www.springframework.org/schema/task
  19. http://www.springframework.org/schema/task/spring-task-4.0.xsd
  20. http://code.alibabatech.com/schema/dubbo
  21. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  22. <!--静态化的服务 -->
  23. <bean id="staticPageService" class="cn.dapeng.core.service.staticpage.StaticPageServiceImpl">
  24. <!-- 驱springmvc提供的freemarkerConfigurer -->
  25. <!-- 可以把bean写到property里一层,也可以通过ref引入 -->
  26. <property name="freeMarkerConfigurer">
  27. <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  28. <!-- 设置模板所在文件夹 -->
  29. <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
  30. <!-- 设置默认编码 -->
  31. <property name="defaultEncoding" value="UTF-8"/>
  32. </bean>
  33. </property>
  34. </bean>
  35. </beans>

3、产品上架时,需要将产品的ID群发给各个服务器,这里产品的service配置改为主题


   
   
  1. <bean id="jmsTempalte" class="org.springframework.jms.core.JmsTemplate">
  2. <!-- 注入Spring的工厂 -->
  3. <property name="connectionFactory" ref="connectionFactory"/>
  4. <!-- JmsTempalte操作某个通道,把通道名称给它,即配置默认目标:商品id 如果用其他目标则jmsTemplate.send(destination, messageCreator)-->
  5. <property name="defaultDestinationName" value="productId"/>
  6. <!--默认不设置是队列 ; 设置就是发送主题 -->
  7. <property name="pubSubDomain" value="true"/>
  8. </bean>

4、生成静态页面的项目中,需要监听器,监听activeMQ发送过来的产品ID,编写mq.xml,配置监听类,配置接收消息为主题


   
   
  1. <!-- 消息处理类 -->
  2. <bean id="customMessageListener" class="cn.dapeng.core.mq.CustomMessageListener"/>
  3. <!--Spring监听MQ 没人需要调它,没人需要注入他,所以不要id -->
  4. <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  5. <!--1、连接Mq进行监听 -->
  6. <property name="connectionFactory" ref="connectionFactory"/>
  7. <!--2、监听的目标地点 -->
  8. <property name="destinationName" value="productId"/>
  9. <!--3、接收消息 -->
  10. <property name="messageListener" ref="customMessageListener"/>
  11. <!--4、默认不设置是队列 ; 设置就是发送主题 -->
  12. <property name="pubSubDomain" value="true"/>
  13. </bean>
5、生成静态页面的项目中,监听类CustomMessageListener,监听得到产品ID,并且根据id查出详情列表需要的信息,然后放到root中。

   
   
  1. public class CustomMessageListener implements MessageListener {
  2. @Autowired
  3. private StaticPageService staticPageService;
  4. @Autowired
  5. private CmsService cmsService;
  6. @Override
  7. public void onMessage(Message message) {
  8. // TODO Auto-generated method stub
  9. ActiveMQTextMessage aMessage = (ActiveMQTextMessage) message;
  10. try {
  11. String productId = aMessage.getText();
  12. System.out.println("cms:"+productId);
  13. //数据模型
  14. Map <String, Object> root = new HashMap <>();
  15. // 查询商品
  16. Product product = cmsService.selectProductById(Long.parseLong(productId));
  17. // 查询库存
  18. List <Sku> skus = cmsService.selectSkuListByProductId(Long.parseLong(productId));
  19. // 去掉颜色重复
  20. Set <Color> colors = new HashSet <Color>();
  21. for (Sku sku : skus) {
  22. colors.add(sku.getColor());
  23. }
  24. root.put("product", product);
  25. root.put("skus", skus);
  26. root.put("colors", colors);
  27. staticPageService.productStaticPage(root, productId);
  28. //solrService.
  29. } catch (JMSException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33. }
  34. }
6、staticPageService的实现类中生成对应产品的静态页面

   
   
  1. * 静态化服务类
  2. * @author Administrator
  3. *
  4. */
  5. public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{
  6. //声明(注入 freeMarkerConfigurer 得到 conf)
  7. private Configuration conf;
  8. //private FreeMarkerConfigurer freeMarkerConfigurer;
  9. public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
  10. this.conf = freeMarkerConfigurer.getConfiguration();
  11. }
  12. //静态化程序
  13. @Override
  14. public void productStaticPage(Map <String, Object> root, String id) {
  15. //获取的绝对路径E:\eclipse-micservice\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\
  16. // wtpwebapps\babasport-service-cms/html/product/12131313.html
  17. String path = getPath("/html/product/"+id+".html");
  18. File f = new File(path);
  19. File parentFile = f.getParentFile();
  20. if (!parentFile.exists()) {
  21. parentFile.mkdirs();
  22. }
  23. //加载目录下指定的模板文件
  24. Writer out = null;
  25. try {
  26. //读取模板(UTF-8)
  27. Template template = conf.getTemplate("productDetail.html");
  28. //输入流 写UTF-8
  29. out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
  30. // 处理process
  31. template.process(root, out);
  32. } catch (Exception e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }finally {
  36. try {
  37. if (null != out) {
  38. out.close();
  39. }
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45. // 获取项目应用的路径
  46. public String getPath(String name) {
  47. return servletContext.getRealPath(name);
  48. }
  49. //声明
  50. private ServletContext servletContext;
  51. @Override
  52. public void setServletContext(ServletContext servletContext){
  53. this.servletContext = servletContext;
  54. }
  55. }
7、静态页面生成后,修改链接地址,点击详情页面跳转到对应的静态页面

   
   
  1. <div class="p-name p-name-type-2">
  2.      <a title="满129立减10,199减20优惠券,支持货到付款" href="javascript:;" onclick="window.open('http://localhost:8084/html/product/${product.id}.html')">
  3.          <em>${product.name } </em>
  4.      </a>
  5. </div>
8、结束,启动项目去验证










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值