foot.ftl 尾
head.ftl 头
item.flt item的详情页面
Configuration 配置
TaglibFactory 标签工厂
template 模板
provided 假如 也就是发布的时候不带有 受保护的 可以防止和tomcat内置的冲突
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
private ServletContext servletContext;
ServletContextAware接口
初始化 ServletContext
@Override
public void setServletConfig(ServletConfig servletConfig) {
this.servletContext = servletContext;
}
解释原理:
//4.创建输出流
Writer out = new OutputStreamWriter(new FileOutputStream(new File(realPath)), "utf-8"); //输出一定是UTF-8
http://localhost:8091/goods/page.do?goodsId=149187842867976
下面是完整源码:
1 package cn.itcast.core.service; 2 3 import cn.itcast.core.dao.good.GoodsDao; 4 import cn.itcast.core.dao.good.GoodsDescDao; 5 import cn.itcast.core.dao.item.ItemCatDao; 6 import cn.itcast.core.dao.item.ItemDao; 7 import cn.itcast.core.pojo.good.Goods; 8 import cn.itcast.core.pojo.good.GoodsDesc; 9 import cn.itcast.core.pojo.item.Item; 10 import cn.itcast.core.pojo.item.ItemQuery; 11 import com.alibaba.dubbo.config.annotation.Service; 12 import freemarker.template.Configuration; 13 import freemarker.template.Template; 14 import org.springframework.beans.factory.annotation.Autowired; 15 import org.springframework.web.context.ServletContextAware; 16 import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; 17 18 import javax.servlet.ServletContext; 19 import java.io.*; 20 import java.util.HashMap; 21 import java.util.List; 22 import java.util.Map; 23 @Service 24 public class PageServiceImpl implements PageService ,ServletContextAware { 25 26 @Autowired 27 private GoodsDao goodsDao;//商品 28 29 @Autowired 30 private GoodsDescDao goodsDescDao;//商品详情 31 32 @Autowired 33 private ItemDao itemDao;//库存集合 34 35 @Autowired 36 private ItemCatDao itemCatDao;//分类数据 37 38 @Autowired 39 private FreeMarkerConfigurer freemarkerConfig; 40 41 private ServletContext servletContext; 42 43 /** 44 * 点击页面单个商品,根据商品id获取生成静态页面所需要的所有的数据 45 * 包括,商品,商品详情,库存集合,分类数据 46 */ 47 @Override 48 public Map<String, Object> findGoodsData(Long goodsId) { 49 //1.根据商品id获取商品对象 50 Goods goods = goodsDao.selectByPrimaryKey(goodsId); 51 //2.根据商品id获取商品详情对象 52 GoodsDesc goodsDesc = goodsDescDao.selectByPrimaryKey(goodsId); 53 54 //3.根据商品id获取库存集合对象 55 ItemQuery query = new ItemQuery(); 56 ItemQuery.Criteria criteria = query.createCriteria(); 57 criteria.andGoodsIdEqualTo(goodsId); 58 List<Item> itemList = itemDao.selectByExample(query); 59 //4.根据商品中对应的分类id找到对应的分类对象 60 //5.将查询出来的数据封装 封装后返回 61 Map<String, Object> rootMap = new HashMap<>(); 62 if (goods != null) { 63 String itemCat1 = itemCatDao.selectByPrimaryKey(goods.getCategory1Id()).getName(); 64 rootMap.put("itemCat1", itemCat1); 65 String itemCat2 = itemCatDao.selectByPrimaryKey(goods.getCategory2Id()).getName(); 66 rootMap.put("itemCat2", itemCat2); 67 String itemCat3 = itemCatDao.selectByPrimaryKey(goods.getCategory3Id()).getName(); 68 rootMap.put("itemCat3", itemCat3); 69 } 70 71 72 rootMap.put("goods", goods); 73 rootMap.put("goodsDesc", goodsDesc); 74 rootMap.put("itemList", itemList); 75 return rootMap; 76 } 77 78 @Override 79 public void createStaticPage(Long goodsId, Map<String, Object> rootMap) throws Exception { 80 //获取模板初始化对象 81 Configuration conf = freemarkerConfig.getConfiguration(); 82 //家在模板 获取模板对象 83 Template template = conf.getTemplate("item.ftl"); 84 //3.设置生成静态化页面的位置 85 String path = goodsId + ".html"; 86 //将相对路径转化为绝对路径 87 88 String realPath = getRealPath(path); 89 System.out.println("==相对路径:"+path); 90 //4.创建输出流 91 Writer out = new OutputStreamWriter(new FileOutputStream(new File(realPath)), "utf-8"); 92 //5.生成 93 template.process(rootMap,out); 94 //6.关闭流 95 out.close(); 96 } 97 98 /** 99 * 将相对路径转化为绝对路径 100 * @param path 例如xxx.html 101 * @return 例如:c://xx//xxx 102 */ 103 private String getRealPath(String path) { 104 String realPath = servletContext.getRealPath(path); 105 return realPath; 106 } 107 /** 108 * spring框架初始化了 ServletContext 109 * @param 110 */ 111 112 113 @Override 114 public void setServletContext(ServletContext servletContext) { 115 this.servletContext = servletContext; 116 } 117 } 118 119 然后到manager更新调用 120 @RequestMapping("/updateStatus") 121 public Result updateStatus(Long[] ids, String status) { 122 try { 123 //1.到数据库中更新商品的审核状态 124 125 goodsService.updateStatus(ids, status); 126 //2.如果审核通过 根据商品id查询商品的详细数据 然后普放入solr索引库供搜索使用 127 if ("1".equals(status) && ids != null) { 128 for (Long goodsId : ids) { 129 //根据商品id查询商品详细数据探后放入到solr索引库供查询使用 130 solrManagerService.addItemToSolr(goodsId); 131 //根据查询到的商品id获取品性详细数据生成商品静态化页面 132 Map<String, Object> rootMap = pageService.findGoodsData(goodsId); 133 pageService.createStaticPage(goodsId, rootMap); 134 } 135 }
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--<context:component-scan base-package="com.pinyougou.page.service.impl"/>--> <dubbo:protocol name="dubbo" port="20885"></dubbo:protocol> <dubbo:application name="pinyougou-page-service"/> <dubbo:registry address="zookeeper://192.168.200.128:2181"/> <dubbo:annotation package="cn.itcast.core,service" /> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <!--配置模板所在文件牡蛎位置--> <property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <!--配置字符集编码为UTF-8--> <property name="defaultEncoding" value="UTF-8" /> </bean> </beans>
配置文件applicationContext-tx.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 开启事务控制的注解支持 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>