1.创建Web项目freeMarkerDemo。

2.添加jar包---freemarker-2.3.9.jar。

3.在WebContent目录下新建templates文件夹,用于放置模板文件ftl。

4.在templates目录下新建hello.ftl。

<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>hello ${user}!</h1>
</body>
</html>

5.在src目录下创建Hello.java文件。

package example;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map; public class Hello extends HttpServlet {
private Configuration cfg; public void init(){
cfg = new Configuration(); //设置FreeMarker的模版文件位置---此处的templates就是上面建的那个文件夹
cfg.setServletContextForTemplateLoading(getServletContext(),"templates");
} public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
//数据模型
Map root = new HashMap();
root.put("user", "xin"); //模版文件
Template temp =cfg.getTemplate("hello.ftl"); response.setContentType("text/html; charset=" + temp.getEncoding()); PrintWriter out = response.getWriter(); //模版+数据模型=输出
try{
temp.process(root,out);
}catch(TemplateException e){
e.printStackTrace();
}
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}

6.配置web.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>freeMarkerDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>example.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello.do</url-pattern>
</servlet-mapping>
</web-app>

7.新建引导页面index.html。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head> <body>
点击下面链接看看效果:
<hr>
<a href="hello.do">调用Hello模板</a>
</body>
</html>

8.把项目部署到tomcate服务器,即可运行。

项目工作流程:首先看到index页面,发现程序跳转到hello.do;然后通过web.xml找到执行主体example.Hello,即Hello.java;执行该程序将数据user输出到模板文件hello.ftl中。

 

作为入门,我们来快速了解三个最为常见的指令---if,list,include

1.if指令

使用if指令可以有条件地跳过模板的一部分。

用法:<#if condition>…</#if>

使用<#else>标签可以指定当条件为假时程序执行的内容。

用法:<#if condition>…<#else>…</#if>

例子,修改hello.ftl

<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>
hello ${user}!
<#if user=="xin">
,my best friend!!!
<#else>
,happy new year!!!
</#if>
</h1>
</body>
</html>

2.list指令

当需要用列表来遍历集合的内容时,list指令是非常好用的。

list指令的一般格式为:

<#list sequence as loopVariable>repeatThis</#list>

repeatThis部分将会在给定的sequence遍历时在每项中重复,从第一项开始,一个接着一个。在所有的重复中,loopVariable将持有当前项的值。这个循环变量仅在于<#list …>和</#list>标签之间。

例子,假设有一组animals数据,如下:

  

java语句为:

List<Animal> animals = new ArrayList<Animal>();

Animal mouse = new Animal("mouse","small",50);
Animal elephant = new Animal("elephant","large",5000);
Animal python = new Animal("python","medium",4999); animals.add(mouse);
animals.add(elephant);
animals.add(python); Map<String, List<Animal>> root = new HashMap<String, List<Animal>>();
root.put("animals", animals);

其中Animal是自定义的一个实体类。

修改hello.ftl,以list的方式遍历animals列出所有的animal

<#list animals as being>
<h1>${being.name}---${being.size}---${being.price}</h1>
</#list>

3.include指令

使用include指令,可以在当前的模板中插入其他文件的内容。

用法:<#include “/a.html”>

在templates目录下新建文件a.html,内容为:

<hr>
<h3>Hello World~~~</h3>

当需要用到这个文件时,可以使用include指令来实现插入。

修改hello.ftl

<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>hello ${user}!</h1>
<#include "/a.html">
</body>
</html>

运行后将会在页面中显示出a.html中的内容。

 

上面的图显示的是一组以sequence序列存储的数据。

下面给出一组以hash表的形式存储的数据:

java语句为:

Map mouse = new HashMap();
mouse.put("size", "small");
mouse.put("price", 50); Animal elephant = new Animal("large",5000);
Animal python = new Animal("medium",4999); Map animals = new HashMap();
animals.put("mouse", mouse);
animals.put("elephant", elephant);
animals.put("python", python); Map<String, Object> root = new HashMap<String, Object>();
root.put("animals", animals);
root.put("test", "It is a test"); Map whatnot = new HashMap();
whatnot.put("because", "don't know");
root.put("whatnot", whatnot);

freeMarker入门示例的更多相关文章

  1. Freemarker 入门示例(zhuan)

    http://cuisuqiang.iteye.com/blog/2031768 ************************************ 初步学习freemarker ,先做一个简单 ...

  2. Freemarker 入门示例

    初步学习freemarker ,先做一个简单的HelloWord程序! 新建一个WEB工程,下载(我使用的是freemarker-2.3.20)freemarker并导入freemarker.jar, ...

  3. 1.【转】spring MVC入门示例(hello world demo)

    1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...

  4. 【转】spring MVC入门示例(hello world demo)

    部分内容来自网络:<第二章 Spring MVC入门 —— 跟开涛学SpringMVC > 1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web M ...

  5. [WCF编程]1.WCF入门示例

    一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...

  6. Maven入门示例(3):自动部署至外部Tomcat

    Maven入门示例(3):自动部署至外部Tomcat 博客分类:  maven 2012原创   Maven入门示例(3):自动部署至外部Tomcat 上一篇,介绍了如何创建Maven项目以及如何在内 ...

  7. 【java开发系列】—— spring简单入门示例

    1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...

  8. Spring MVC 入门示例讲解

    在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...

  9. Couchbase之个人描述及入门示例

    本文不打算抄袭官方或者引用他人对Couchbase的各种描述,仅仅是自己对它的一点理解(错误之处,敬请指出),并附上一个入门示例. ASP.NET Web项目(其他web开发平台也一样)应用规模小的时 ...

随机推荐

  1. qt 4.8.5 vs 2012编译

    Download Qt 4.8.3 source code from http://qt-project.org/downloads Go to mkspecs\win32-msvc2010. Ope ...

  2. Spring Data Redis 2.x 中 RedisConfiguration 类的新编写方法

    在 Spring Data Redis 1.x 的时候,我们可能会在项目中编写这样一个RedisConfig类: @Configuration @EnableCaching public class ...

  3. win7无法安装Microsoft .NET Framework 4.5\4.0

    在安装一些软件的时候,要求安装环境下必须首先安装有Microsoft .NET Framework 4.5\4.0,可是下载了安装包却怎么也安装不了,安装到一定进度就失败了,反复安装重启,都没有成功, ...

  4. 第一百九十三节,jQuery EasyUI,Draggable(拖动)组件

    Draggable(拖动)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Draggable(拖动)组件的使用方法,这个组件不依赖于其 他组 ...

  5. virtualenv使用说明

    创建虚拟环境virtualenv [虚拟环境名称] 如:virtualenv ENV 启动虚拟环境 cd ENV source ./bin/activate 注意此时命令行会多一个(ENV),ENV为 ...

  6. phpcms v9 配置sphinx全文索引教程

    英文介绍:http://www.sphinxsearch.com/docs/manual-0.9.9.html 一.首先需要在服务器上安装sphinx 在Windows上安装sphinx     1. ...

  7. Unity3D学习笔记——NGUI之UIPopupList

    UIPopupList:将这个组件添加到Button上,即可构建一个下拉列表. 效果图如下: 一:使用步骤 1.首先创建一个Sprite作为button. 2.创建一个label,用于显示当前选择的选 ...

  8. hibernate Session一级缓存 应该注意的地方

    Session缓存 Hibernate的一级缓存是由Session提供的,因此它存在于Session的整个生命周期中,当程序调用save()/update()/saveOrupdate()/get() ...

  9. linux 复制粘贴

    如果想把文件内的第三行内容黏贴到第十五行: 1.进入你的文件,(处于命令模式,而不是编辑模式) 2.将你的光标移到你的第三行的开头(用方向键) 3,按下v,(你发现切换到可视模式了),按方向键,知道选 ...

  10. Win7 maven安装及配置

    1. 前期准备 ① jdk 1.8 ② maven 3.5.4 下载地址:http://maven.apache.org/download.cgi 2. 配置maven环境变量 ① maven解压到指 ...