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

8 篇文章 0 订阅
7 篇文章 0 订阅

pom.xml

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>

后台代码

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<TbItem> 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\\     //本地磁盘中要存在此目录
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值