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; //获取服务器路径
}
}
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);
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语法即可