本文共 16748 字,大约阅读时间需要 55 分钟。
本博客介绍,指定条件下生成静态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的标签,如:遍历用
<#list persons as person> ${person_index} ${person}
引入其他页面,如上下左右等边角页面用
<#include "commons/footer.html" />
加入UTF-8
2、编写freemarker.xml配置文件,放在生成静态页面的项目中
3、产品上架时,需要将产品的ID群发给各个服务器,这里产品的service配置改为主题
4、生成静态页面的项目中,需要监听器,监听activeMQ发送过来的产品ID,编写mq.xml,配置监听类,配置接收消息为主题
5、生成静态页面的项目中,监听类CustomMessageListener,监听得到产品ID,并且根据id查出详情列表需要的信息,然后放到root中。
public class CustomMessageListener implements MessageListener { @Autowired private StaticPageService staticPageService; @Autowired private CmsService cmsService; @Override public void onMessage(Message message) { // TODO Auto-generated method stub ActiveMQTextMessage aMessage = (ActiveMQTextMessage) message; try { String productId = aMessage.getText(); System.out.println("cms:"+productId); //数据模型 Map6、staticPageService的实现类中生成对应产品的静态页面root = new HashMap <>(); // 查询商品 Product product = cmsService.selectProductById(Long.parseLong(productId)); // 查询库存 List skus = cmsService.selectSkuListByProductId(Long.parseLong(productId)); // 去掉颜色重复 Set colors = new HashSet (); for (Sku sku : skus) { colors.add(sku.getColor()); } root.put("product", product); root.put("skus", skus); root.put("colors", colors); staticPageService.productStaticPage(root, productId); //solrService. } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
* 静态化服务类 * @author Administrator * */ public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{ //声明(注入 freeMarkerConfigurer 得到 conf) private Configuration conf; //private FreeMarkerConfigurer freeMarkerConfigurer; public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) { this.conf = freeMarkerConfigurer.getConfiguration(); } //静态化程序 @Override public void productStaticPage(Map7、静态页面生成后,修改链接地址,点击详情页面跳转到对应的静态页面root, String id) { //获取的绝对路径E:\eclipse-micservice\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\ // wtpwebapps\babasport-service-cms/html/product/12131313.html String path = getPath("/html/product/"+id+".html"); File f = new File(path); File parentFile = f.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } //加载目录下指定的模板文件 Writer out = null; try { //读取模板(UTF-8) Template template = conf.getTemplate("productDetail.html"); //输入流 写UTF-8 out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8"); // 处理process template.process(root, out); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if (null != out) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } // 获取项目应用的路径 public String getPath(String name) { return servletContext.getRealPath(name); } //声明 private ServletContext servletContext; @Override public void setServletContext(ServletContext servletContext){ this.servletContext = servletContext; } }
8、结束,启动项目去验证
转载地址:http://qnse.baihongyu.com/