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的配置与使用的更多相关文章

  1. freemarker属性配置

    freemarker属性配置: spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attr ...

  2. springboot集成freemarker属性配置(不知道是针对于某个版本,2.0后有变动)

    freemarker属性配置 freemarker属性配置: spring.freemarker.allow-request-override=false # 设置是否允许HttpServletReq ...

  3. Java: FreeMarker的配置和使用

    初学什么都不可以忽略的地方就是这个东西的官方网站:http://freemarker.org/.下载或者API都可以参考这里. FreeMarker是什么 非常的简单明了.FreeMarker是一个j ...

  4. freemarker springmvc配置异常

    异常信息 java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashMo ...

  5. freemarker配置信息

    <!--  <!– freemarker的配置 –> <bean id="freemarkerConfigurer" class="org.spr ...

  6. springMVC配置freemarker

    这里呢,我首先来说明一下写该篇的目的. 我最近要用到freemarker因此研究了一下这个东西. 先来说说如何配置吧. 1.jar包.地址见下链接. http://pan.baidu.com/s/1j ...

  7. SpringMvc多视图配置(jsp、velocity、freemarker) velocity在springmvc.xml配置VelocityViewResolver,VelocityConfigurer,FreeMarkerConfigurer,FreeMarkerViewResolver

    ?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springf ...

  8. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  9. spring配置freemarker

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

随机推荐

  1. Response.End方法

    文章:在try...catch语句中执行Response.End()后如何停止执行catch语句中的内容 调用Response.End()方法能保证,只输出End方法之前的内容. 调用Context. ...

  2. # ML学习小笔记—Classification

    关于本课程的相关资料http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html 通过模型可以分类输入,此时根据分类结果的正确与否会有一个Loss函数.找 ...

  3. java — 线程池

    线程池的作用       线程池作用就是限制系统中执行线程的数量.     根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少了浪费了系统资源,多了造成系统拥挤效率不高.用线程池控 ...

  4. lintcode-13-字符串查找

    字符串查找 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 说明 ...

  5. iOS- <项目笔记>UI控件常见属性总结

    1.UIView // 如果userInteractionEnabled=NO,不能跟用户交互 @property(nonatomic,getter=isUserInteractionEnabled) ...

  6. TCP系列30—窗口管理&流控—4、Cork算法

    一.Cork算法概述 Cork算法与Nagle算法类似,也有人把Cork算法称呼为super-Nagle.Nagle算法提出的背景是网络因为大量小包小包而导致利用率低下产生网络拥塞,网络发生拥塞的时候 ...

  7. git工具SourceTree工作流

    分支模型 master 用来最终上线的分支,最终发布版本,整个项目中有且只有一个 develop 项目中用来开发的分支,原则上项目中有且只有一个,develop 分支下面的分支是经常变化的,会创建新的 ...

  8. 编译android6.0错误recipe for target 'out/host/linux-x86/obj/lib/libart.so' failed

    转自:http://blog.csdn.net/ztguang/article/details/52856076 trip: libpagemap_32 (out/target/product/xx/ ...

  9. linux+Nginx+Mysql+PHP环境下,安装mysqli模块

    奶奶的腿儿啊,太不易了.倒腾了小半天儿,写此随笔,待后查. 阿里云ecs中,安装phpcms,出现了一个问题:环境检测的时候,一直提示 Mysqli扩展没开启. 老夫哪儿特么会这么专业的啊...能咋办 ...

  10. div、span绑定内容改变事件

    内容改变事件onchange只适用于form表单标签(input.select.textarea) 当需要对div.span标签进行内容改变监听则无法适用,查阅了一些资料发现jquery有针对的方法, ...