下面演示一个简单的使用项目过程:
1. 使用myeclipse创建一个web项目testFM
2. 把下载到的jar包(freemarker-2.3.9.jar)放到/WebRoot/WEB-INF/lib目录下。下载地址:http://freemarker.sourceforge.net/freemarkerdownload.html (注:官方网站.org的经常打不开)
3. 在WebRoot下面新建templates文件包,然后在里面新建扩展名为ftl的模板。
a) 创建hello.ftl内容如下:
<html>
<head>
<title>hello!</title>
</head>
<body>
<h1>hello ${user}!</h1>
</body>
</html>

4. 在src目录下创建example包 和Hello.java文件,代码如下:
package example;
import java.io.*;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import freemarker.template.*;

public class Hello extends HttpServlet{
private Configuration cfg;

public void init()
{
   //初始化FreeMarker配置
        //创建一个Configuration实例
        cfg = new Configuration();
        //设置FreeMarker的模版文件位置
   cfg.setServletContextForTemplateLoading(getServletContext(),"templates");
}

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
   //建立数据模型
   Map root = new HashMap();
   //放入对应数据key value
   root.put("user","csdn");
   //取得模版文件
   Template t =cfg.getTemplate("hello.ftl");
   //开始准备生成输出
        //使用模版文件的charset作为本页面的charset
        //使用text/html MIME-type
        response.setContentType("text/html; charset=" + t.getEncoding());
   PrintWriter out = response.getWriter();
  
   //合并数据模型和模版,并将结果输出到out中
   try
   {
    t.process(root,out);// 用模板来开发servlet可以只在代码里面加入动态的数据
   }
   catch(TemplateException e)
   {
    throw new ServletException("处理Template模版中出现错误", e);
   }
}
}

5.配置web.xml 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>example.Hello</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
      <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

6.在WebRoot下面新建引导页面index.html,代码如下:

<html>
<head>
<title>Hello FreeMarker Example</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
</head>
<body>
点击下面链接看看效果:
<hr>
<a href="hello.do">调用Hello模板</a> 
</body>
</html>

7.把testFM项目部署到tomcate并启动服务。
8.打开浏览器,输入地址:http://localhost:8080/testFM/
9.点击调用Hello模板 链接,如果出现hello csdn!则说明模板调用成功了!

最新文章

  1. jdbc 设置连接支持多条sql
  2. Apache-Tomcat的安装配置
  3. 为什么angularjs使用ui-router时要使用html5Mode?
  4. oracle如何写包
  5. #VSTS定制#全新的模版定制能力
  6. 当元素的样式为display:none时获取他的宽高
  7. MariaDB5.5(mysql)的partitioning设置 for Zabbix3.0
  8. init/main.c
  9. ecshop被加入了黑链
  10. setcookie,getcookie,delcookie,setpostBgPic
  11. (转)FastDFS_v5.05安装配置
  12. JavaScript window.location对象
  13. mysql System Tablespace
  14. 简单的js实现网页时钟
  15. .NET在VS2008中生成DLL并调用
  16. [Swift]LeetCode563. 二叉树的坡度 | Binary Tree Tilt
  17. Dynamics 365执行操作报SQL Server已超时,更改这个超时设置的方法
  18. 设计模式之jdk动态代理模式、责任链模式-java实现
  19. oracle与mysql
  20. 菜鸟教程--AJAX

热门文章

  1. 最牛叉的街机游戏合集 &amp; 模拟器
  2. 不含类解决最后一个li边距问题
  3. 顾维灏谈百度地图数据采集:POI自动处理率达90%
  4. ubuntu安装jdk-6u45-linux-x64-rpm.bin
  5. Java编程思想学习笔记_4(异常机制,容器)
  6. POJ 2063 Investment 完全背包
  7. a链接onclick=&quot;window.location.href=在ie6上面无法执行解决
  8. 转:Teach Yourself Programming in Ten Years——用十年教会自己编程
  9. 全局变量报错:UnboundLocalError: local variable &#39;l&#39; referenced before assignment
  10. JSON和php里的数据序列化