1.在pro.xml配置文件中引入架包

<!--freemarker-->
  <dependency>
   <groupId>freemarker</groupId>
   <artifactId>freemarker</artifactId>
   <version>${freemarker-version}</version>
  </dependency>

2.Freemarker的配置

  1. <?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">
  2.  
  3. <!-- 配置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静态化页面

  1. package cn.itcast.core.service.staticpage;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. import java.io.Writer;
  8. import java.util.Map;
  9.  
  10. import javax.servlet.ServletContext;
  11.  
  12. import org.springframework.web.context.ServletContextAware;
  13. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
  14.  
  15. import freemarker.template.Configuration;
  16. import freemarker.template.Template;
  17.  
  18. /**
  19. * 生成静态页实现类
  20. * @author lx
  21. *
  22. */
  23. public class StaticPageServiceImpl implements StaticPageService,ServletContextAware{
  24.  
  25. private Configuration conf;
  26.  
  27. public void setFreeMarkerConfigurer(FreeMarkerConfigurer freeMarkerConfigurer) {
  28. this.conf = freeMarkerConfigurer.getConfiguration();
  29. }
  30.  
  31. //静态化方法
  32. public void productIndex(Map<String,Object> root,Integer id){
  33. //String dir = "C:\Users\lx\workspace\babasport12\";
  34. //设置模板的目录
  35. //conf.setDirectoryForTemplateLoading(dir);
  36.  
  37. //输出流 从内存写出去 磁盘上
  38. Writer out = null;
  39. try {
  40. //读进来 UTF-8 内存中
  41. Template template = conf.getTemplate("productDetail.html");
  42. //获取Html的路径
  43. String path = getPath("/html/product/" + id + ".html");//278.html
  44.  
  45. File f = new File(path);
  46. File parentFile = f.getParentFile();
  47. if(!parentFile.exists()){
  48. parentFile.mkdirs();
  49. }
  50. //输出流
  51. out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
  52. //处理模板
  53. template.process(root, out);
  54. } catch (Exception e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }finally{
  58. if(out != null){
  59. try {
  60. out.close();
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. }
  66. }
  67. }
  68. //获取路径
  69. public String getPath(String name){
  70. return servletContext.getRealPath(name);
  71. }
  72.  
  73. private ServletContext servletContext;
  74.  
  75. @Override
  76. public void setServletContext(ServletContext servletContext) {
  77. this.servletContext = servletContext;
  78. }
  79. }

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. File Searching

    Description Have you ever used file searching tools provided by an operating system? For example, in ...

  2. oracle数据库之PL/SQL 块结构和组成元素

    一.PL/SQL 块 (一)PL/SQL 程序由三个块组成,即声明部分.执行部分.异常处理部分 PL/SQL 块的结构如下: 1.DECLARE /* 声明部分: 在此声明 PL/SQL 用到的变量, ...

  3. 总结get和post区别

    参考博文: 浅谈HTTP中Get与Post的区别 1. 数据传递方向: Get是向服务器发索取数据的一种请求,Post是向服务器提交数据的一种请求 (都是请求,并不是一个取一个发) Get:①用于获取 ...

  4. 关于GenericJDBCException的问题

    在spring和hibernate整合的初步阶段,还没有编辑hibernate.cfg.xml这个文件,只有一个beans.xml文件.此时遇到了一个bug. Exception in thread ...

  5. II 3.1 连接到服务器

    II 3.1 连接到服务器 package socket; import java.io.IOException; import java.io.InputStream; import java.ne ...

  6. 一个demo让你彻底理解Android中触摸事件的分发

    注:本文涉及的demo的地址:https://github.com/absfree/TouchDispatch 1. 触摸动作及事件序列 (1)触摸事件的动作 触摸动作一共有三种:ACTION_DOW ...

  7. <Effective C++>读书摘要--Designs and Declarations<一>

    <Item 18> Make interfaces easy to use correctly and hard to use incorrectly 1.That being the c ...

  8. 最近面试前端面试题整理(css部分)

    对最近面试的面试题坐下总结: 一,css部分 1,html元素的垂直居中 答案: <div id="box"> <div> 测试 </div> ...

  9. cacti 安装perl 和XML::Simple

    一.安装perl #tar zxvf perl-5.20.1.tar.gz #cd perl-5.20.1  #./Configure -de  #make  #make test  #make in ...

  10. tcp发送缓冲区中的数据都是由产生数据的进程给推送到ip层还是有定时任务触发?

    和几个变量有非常大的关系 发送缓冲区的大小,如何单独设置一个socket的发送缓冲区 socketopt 发送缓冲区中的数据,如果被拥塞窗口限制住了,那么这些数据可能就放在tcpbuffer里的,此时 ...