spring中freemarker生成静态页面

1.导入相应jar包freemarker-2.3.9.jar

<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.9</version>
</dependency>

2.在spring中配置freemarker

<!-- 配置freemarker -->
<bean id="staticPageService" class="com.babasport.core.service.staticPage.StaticPageServiceImpl">
<property name="configurer">
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/"/> //指定freemarker模板的目录
<property name="defaultEncoding" value="UTF-8"/> //设置编码格式
</bean>
</property>
</bean>

3.编写service实现类


package com.babasport.core.service.staticPage;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;


import javax.servlet.ServletContext;


import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;


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


/**
 * 生成静态页实现类
 * <p>Company: www.gsafety.com</p> 
 * @author 汪敏
 * @date 2017年10月22日下午2:04:20
 * @version 1.0
 */
public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{


private Configuration configuration;
private ServletContext servletContext;


public void setConfigurer(FreeMarkerConfigurer configurer) {
this.configuration = configurer.getConfiguration();
}


@Override
public void productIndex(Map<String, Object> rootMap,Integer id) {
//输出流,从内存写入磁盘设置utf-8格式
Writer out = null;
try {
//获取存放HTML的路径
String path = servletContext.getRealPath("/html/product/" + id + ".html");
File file = new File(path);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
//从磁盘度日内存 设置utf-8格式
Template template = configuration.getTemplate("productDetail.html");
template.process(rootMap, out);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext; //获取服务器路径
}


}

4.调用方法,生成静态页面


// 静态化页面
Map<String, Object> rootMap = new HashMap<>();
product = productService.getProductByKey(id);
rootMap.put("product", product);

SkuQuery skuQuery = new SkuQuery();
skuQuery.setProductId(id);
List<Sku> skuList = skuService.getStock(id);
//去重复
List<Color> colors = new ArrayList<>();
List<String> colorNames = new ArrayList<>();
for (Sku sku : skuList) {
String colorName = sku.getColor().getName();
if (!colorNames.contains(colorName)) {
colorNames.add(colorName);
colors.add(sku.getColor());
}
}

rootMap.put("skuList", skuList);
rootMap.put("colors", colors);
staticPageService.productIndex(rootMap, id);

5.修改模板页面

<#list skuList as sku>
<#List>
将模板页面语法改为freemarker语法即可


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值