freeMarker入门示例
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入门示例的更多相关文章
- Freemarker 入门示例(zhuan)
http://cuisuqiang.iteye.com/blog/2031768 ************************************ 初步学习freemarker ,先做一个简单 ...
- Freemarker 入门示例
初步学习freemarker ,先做一个简单的HelloWord程序! 新建一个WEB工程,下载(我使用的是freemarker-2.3.20)freemarker并导入freemarker.jar, ...
- 1.【转】spring MVC入门示例(hello world demo)
1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于 ...
- 【转】spring MVC入门示例(hello world demo)
部分内容来自网络:<第二章 Spring MVC入门 —— 跟开涛学SpringMVC > 1. Spring MVC介绍 Spring Web MVC是一种基于Java的实现了Web M ...
- [WCF编程]1.WCF入门示例
一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...
- Maven入门示例(3):自动部署至外部Tomcat
Maven入门示例(3):自动部署至外部Tomcat 博客分类: maven 2012原创 Maven入门示例(3):自动部署至外部Tomcat 上一篇,介绍了如何创建Maven项目以及如何在内 ...
- 【java开发系列】—— spring简单入门示例
1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...
- Spring MVC 入门示例讲解
在本例中,我们将使用Spring MVC框架构建一个入门级web应用程序.Spring MVC 是Spring框架最重要的的模块之一.它以强大的Spring IoC容器为基础,并充分利用容器的特性来简 ...
- Couchbase之个人描述及入门示例
本文不打算抄袭官方或者引用他人对Couchbase的各种描述,仅仅是自己对它的一点理解(错误之处,敬请指出),并附上一个入门示例. ASP.NET Web项目(其他web开发平台也一样)应用规模小的时 ...
随机推荐
- int和Integer差别
种原始数据类型之中的一个. Java为每一个原始类型提供了封装类.Integer是java为int提供的封装类. 原始数据类型包含byte.int.char.long.float.double.boo ...
- ubuntu下软件安装
1. 软件源:ubuntu.cn99.com/ubuntu2. 安装vncviewer sudo apt-get install vncviewer3. aptitude sudo ap ...
- 2017-5-14 湘潭市赛 Longest Common Subsequence 想法题
Longest Common Subsequence Accepted : Submit : Time Limit : MS Memory Limit : KB Longest Common Subs ...
- Unity3D学习笔记——UIScrollBar和UIScrollView使用
UIScrollBar和UIScrollView结合使用效果图如下: 一:使用步骤 1.创建一个UIScrollView 2.然后创建一个UIScrollBar 3.打开UIScrollView ...
- Unity3D学习笔记——游戏组件之Mesh(网格组件)
Mesh:网格组件.主要用于设置外形和外表. Mesh Filter:网格过滤器.就是为游戏对象添加一个外形. 例:设置外形为Sphere 如果获取的网格拥有蒙皮信患,Unity将自动创建一个skn ...
- MEF笔记 之延迟加载
文章参考:在MEF中实现延迟加载部件 作者:TianFang 仅有一个服务提供者时候 using System; using System.Collections.Generic; using Sy ...
- 根据funID,personID获取最新规划包项目相关信息
1.定义:根据funID,personID获取最新规划包项目相关信息(code projecttype(阶段) Pname(code+name) projectID) 项目表tbl_cfg_Proje ...
- requirejs 第一个实例
介绍:requirejs 是实现了模块化加载和按需加载的js库,防止了全局变量的污染. 1.安装 require : 在require 中文网下载 http://www.requirejs.cn/,然 ...
- mysqldump: Got error: 1045
问题:最近备份mysql然后在执行mysqldump的时候提示权限和密码有问题 报错: Warning: Using a password on the command line interface ...
- Travel(最短路)
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...