spring中freemarker生成静态页面
来源:互联网 发布:2017人工智能产业规模 编辑:程序博客网 时间:2024/05/16 15:01
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语法即可
阅读全文
0 0
- spring中freemarker生成静态页面
- struts2中使用freemarker 生成静态页面
- struts2中使用freemarker 生成静态页面
- struts2中使用freemarker 生成静态页面
- Freemarker生成静态页面
- freemarker生成静态页面
- freemarker生成静态页面
- FreeMarker生成静态页面
- FreeMarker生成静态页面
- Spring Boot使用freemarker并且生成静态html页面
- 使用freemarker生成静态页面
- struts2+freemarker 生成静态页面
- 用freemarker生成静态页面
- struts2+freemarker 生成静态页面
- freemarker之生成静态页面
- 使用freemarker生成静态页面
- Freemarker生成HTML静态页面
- SpringMVC+Freemarker生成静态页面
- git简介及常用命令
- 一些javaweb开发常用工具类
- python: join函数
- 算法设计 第二次上机 The Unique MST
- 10-12-C++外包面试题
- spring中freemarker生成静态页面
- Hibernate 入门
- 设计模式_责任链模式(20)
- 如何正确地比较两个double类型的数据
- Firefox 恢复关闭的标签页 用什么插件?
- java se--8.多线程-1.基本概念
- 二叉树
- 16.存储过程
- C++程序题总结