使用springboot 集成 freemarker 生成静态页面。

pom.xml

	
			org.springframework.boot
			spring-boot-starter-freemarker
	

后台代码

package org.pinyougou.page.service.api;

import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.pinyougou.page.api.api.ItemPageApi;
import org.pinyougou.page.service.service.TbGoodsDescService;
import org.pinyougou.page.service.service.TbGoodsService;
import org.pinyougou.page.service.service.TbItemCatService;
import org.pinyougou.page.service.service.TbItemService;
import org.pinyougou.sellergoods.api.domain.TbGoods;
import org.pinyougou.sellergoods.api.domain.TbGoodsDesc;
import org.pinyougou.sellergoods.api.domain.TbItem;
import org.pinyougou.sellergoods.api.domain.TbItemExample;
import org.pinyougou.sellergoods.api.domain.TbItemExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Configuration;
import freemarker.template.Template;

@RestController
public class ItemPageControllerApi implements ItemPageApi{


	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	@Value("${pagedir}")
	private String pagedir;
	
	
	@Autowired
	private TbGoodsService goodsService;
	
	@Autowired
	private TbGoodsDescService goodsDescService;
	
	@Autowired
	private TbItemCatService itemCatService;
	
	@Autowired
	private TbItemService itemService;
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean genItemHtml(@RequestParam("goodsId") Long goodsId) {
		System.out.println(goodsId+"211111");
		Configuration configuration = freeMarkerConfigurer.getConfiguration();
		
		try {
			  configuration.setClassForTemplateLoading(this.getClass(), "/static/templates/");
			//ContextLoader loader = new ContextLoader();
			Template template = configuration.getTemplate("item.ftl");
			//创建数据模型
			@SuppressWarnings("rawtypes")
			Map dataModel=new HashMap<>();
			//1.商品主表数据
			TbGoods goods = goodsService.selectByPrimaryKey(goodsId);
			dataModel.put("goods", goods);
			//2.商品扩展表数据
			TbGoodsDesc goodsDesc = goodsDescService.selectByPrimaryKey(goodsId);
			dataModel.put("goodsDesc", goodsDesc);
			//3.读取商品分类
			
			String itemCat1 = itemCatService.selectByPrimaryKey(goods.getCategory1Id()).getName();
			String itemCat2 = itemCatService.selectByPrimaryKey(goods.getCategory2Id()).getName();
			String itemCat3 = itemCatService.selectByPrimaryKey(goods.getCategory3Id()).getName();
			dataModel.put("itemCat1", itemCat1);
			dataModel.put("itemCat2", itemCat2);
			dataModel.put("itemCat3", itemCat3);
			
			//4.读取SKU列表
			TbItemExample example=new TbItemExample();
			Criteria criteria = example.createCriteria();
			criteria.andGoodsIdEqualTo(goodsId);//SPU ID
			criteria.andStatusEqualTo("1");//状态有效			
			example.setOrderByClause("is_default desc");//按是否默认字段进行降序排序,目的是返回的结果第一条为默认SKU
			
			List itemList = itemService.selectByExample(example);
			dataModel.put("itemList", itemList);
			
			Writer out=new FileWriter(pagedir+goodsId+".html");
			
			template.process(dataModel, out);//输出
			out.close();
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} 
		
	}

}

application.properties

#freemarker
spring.freemarker.charset=UTF-8
spring.freemarker.cache=false
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.suffix=.ftl
pagedir=d:\\item\\     //本地磁盘中要存在此目录

你可能感兴趣的:(spring,springboot,maven)