Freemarker的配置与使用
1.在pro.xml配置文件中引入架包
<!--freemarker-->
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker-version}</version>
</dependency>
2.Freemarker的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置Freemarker --> <bean id="staticPageService" class="cn.itcast.core.service.staticpage.StaticPageServiceImpl"> <property name="freeMarkerConfigurer"> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/"/> <property name="defaultEncoding" value="UTF-8"/> </bean> </property> </bean> </beans>
3.Freemarker静态化页面
package cn.itcast.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; /**
* 生成静态页实现类
* @author lx
*
*/
public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{ private Configuration conf; public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
this.conf = freeMarkerConfigurer.getConfiguration();
} //静态化方法
public void productIndex(Map<String,Object> root,Integer id){
//String dir = "C:\Users\lx\workspace\babasport12\";
//设置模板的目录
//conf.setDirectoryForTemplateLoading(dir); //输出流 从内存写出去 磁盘上
Writer out = null;
try {
//读进来 UTF-8 内存中
Template template = conf.getTemplate("productDetail.html");
//获取Html的路径
String path = getPath("/html/product/" + id + ".html");//278.html File f = new File(path);
File parentFile = f.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
//输出流
out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
//处理模板
template.process(root, out);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//获取路径
public String getPath(String name){
return servletContext.getRealPath(name);
} private ServletContext servletContext; @Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}
Freemarker的配置与使用的更多相关文章
- freemarker属性配置
freemarker属性配置: spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attr ...
- springboot集成freemarker属性配置(不知道是针对于某个版本,2.0后有变动)
freemarker属性配置 freemarker属性配置: spring.freemarker.allow-request-override=false # 设置是否允许HttpServletReq ...
- Java: FreeMarker的配置和使用
初学什么都不可以忽略的地方就是这个东西的官方网站:http://freemarker.org/.下载或者API都可以参考这里. FreeMarker是什么 非常的简单明了.FreeMarker是一个j ...
- freemarker springmvc配置异常
异常信息 java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashMo ...
- freemarker配置信息
<!-- <!– freemarker的配置 –> <bean id="freemarkerConfigurer" class="org.spr ...
- springMVC配置freemarker
这里呢,我首先来说明一下写该篇的目的. 我最近要用到freemarker因此研究了一下这个东西. 先来说说如何配置吧. 1.jar包.地址见下链接. http://pan.baidu.com/s/1j ...
- SpringMvc多视图配置(jsp、velocity、freemarker) velocity在springmvc.xml配置VelocityViewResolver,VelocityConfigurer,FreeMarkerConfigurer,FreeMarkerViewResolver
?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springf ...
- springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息
1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...
- spring配置freemarker
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
随机推荐
- Python中的namespace package
在Python 3.3之前,一个目录想被当成package被导入,必须包含__init__.py文件:而在Python 3.3及以后的版本中,__init__.py文件可以不需要,直接使用import ...
- Python练习—函数
1.编写函数f(n),实现输入n的值,求出n的阶乘.然后调用此函数计算1! +2!+3!+……10!的结果,输出到屏幕上. def f(n): count = 1; for i in range(1, ...
- File Searching
Description Have you ever used file searching tools provided by an operating system? For example, in ...
- Calculator Part Ⅰ (代码规范化修改)
GitHub/object-oriented 本次参照的C++代码规范 有一些规范内容在文件中其实并未提及,比如:空格的使用,修改的时候真的是一头雾水--根据文件中的例子,发现了一些文字部分没有提到的 ...
- 二叉搜索树(BST)---python实现
github:代码实现 本文算法均使用python3实现 1. 二叉搜索树定义 二叉搜索树(Binary Search Tree),又名二叉排序树(Binary Sort Tree). 二叉搜 ...
- SVM之问题形式化
>>>SVM之问题形式化 SVM之对偶问题 SVM之核函数 SVM之解决线性不可分 写在SVM之前——凸优化与对偶问题 SVM内容繁多,打算用五篇文章来记述.SVM之问题形式化描述给 ...
- PL/SQL在 win8.1系统下连接Oracle11g没有database处理方法(亲身实验,吐血分享)
一.问题 这里首先说明下我的环境:win8.1(64bit)+oracle11g(64bit)+PL/SQL(32bit).状况是:net manager正常配置,测试也成功,但是用PL/SQL连接的 ...
- DBGrid相关技术整理
DBGrid相关技术整理: 注:对于DBGrid相关属性.方法的学习融入到技术整理过程中 一,多选 设置属性: Options->dgMultiSelect = True; ->dgRow ...
- 【bzoj5001】搞事情 暴力
题目描述 给定一个NM的01矩阵,每次可以选定一个位置,将它和它相邻格子的数取反.问:怎样操作使得所有格子都变为0.当有多组解时,优先取操作次数最小的:当操作次数相同时,优先取字典序最小的. 输入 第 ...
- [洛谷P1452]Beauty Contest
题目大意:给你$n$个点,求出其中最远点的距离 题解:求出凸包,最远点一定都在凸包上,可以对每条边求出最远的点(可以双指针),然后求出和这条边的端点的距离,更新答案 卡点:最开始对每个点求出最远点,但 ...