我的第一个FreeMarker程序

1、java应用程序中
??? 我用maven导入FreeMarker的jar包
???
??? <dependency>
????? <groupid>freemarker</groupid>
????? <artifactid>freemarker</artifactid>
????? <version>2.3.8</version>
??? </dependency>

??? package com.test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;


import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Test {

??? /** *//**
???? * @param args
???? */
??? public static void main(String[] args) {
??????? Configuration configuration = new Configuration();
??????? configuration.setObjectWrapper(new DefaultObjectWrapper());
??????? configuration.setTemplateLoader(new ClassTemplateLoader(Test.class, "/com/test"));
??????? try {
??????????? Template template = configuration.getTemplate("temp.ftl");
??????????? StringWriter writer = new StringWriter();
??????????? Map<string, object=""> context = new HashMap<string, object="">();
???????????
??????????? context.put("message", "我的第一个FreeMarker程序");
???????????
??????????? template.process(context, writer);
??????????? System.out.println(writer.toString());
??????? } catch (IOException e) {
??????????? // TODO Auto-generated catch block
??????????? e.printStackTrace();
??????? } catch (TemplateException e) {
??????????? // TODO Auto-generated catch block
??????????? e.printStackTrace();
??????? }
??? }

}

这里是用java应用程序,需要加入下面两句(设置模板文件载入):
方法一:
? configuration.setObjectWrapper(new DefaultObjectWrapper());
? configuration.setTemplateLoader(new ClassTemplateLoader(Test.class, "/com/test"));

其中"/com/test" 是模板文件所在的文件夹,前面的“/”必须,这里我的模板文件和当前java类在同一路径下,则这样写,如果你的模板文件在src跟目录下,这里只需要写“/”就可以了

context.put("message", "我的第一个FreeMarker程序");
是模板文件中用到的变量,map中的变量可以是javabean,也可以是对象


方法二:
configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates");
这里主要针对Servlet的时候,当然在Servlet中也可以用方法一提到的写法
package com.newegg.lab.freemarker.servlet;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/** *//**
?* Servlet implementation class for Servlet: FreemarkerServlet
?*
?*/
public class FreemarkerServlet extends javax.servlet.http.HttpServlet implements
??????? javax.servlet.Servlet {

??? private Configuration configuration;

??? private Template template;
??? /**//*
???? * (non-Java-doc)
???? *
???? * @see javax.servlet.http.HttpServlet#HttpServlet()
???? */
??? public FreemarkerServlet() {
??????? super();
??? }

??? @Override
??? public void destroy() {
??????? // TODO Auto-generated method stub
??????? configuration = null;
??? }

??? @Override
??? public void init() throws ServletException {
??????? // TODO Auto-generated method stub
??????? configuration = new Configuration();
??????? configuration.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates");
//??????? configuration.setObjectWrapper(new DefaultObjectWrapper());
//??????? configuration.setTemplateLoader(new ClassTemplateLoader(FreemarkerServlet.class, "/"));
??? }

??? /**//*
???? * (non-Java-doc)
???? *
???? * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
???? *????? HttpServletResponse response)
???? */
??? protected void doGet(HttpServletRequest request,
??????????? HttpServletResponse response) throws ServletException, IOException {
??????? // TODO Auto-generated method stub
??????? this.doPost(request, response);
??? }

??? /**//*
???? * (non-Java-doc)
???? *
???? * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
???? *????? HttpServletResponse response)
???? */
??? protected void doPost(HttpServletRequest request,
??????????? HttpServletResponse response) throws ServletException, IOException {
??????? // TODO Auto-generated method stub
??????? Map root = new HashMap();
??????? root.put("message", "Hello World!的的的的");

??????? // Get the templat object
??????? Template t = configuration.getTemplate("test.ftl");

??????? // Prepare the HTTP response:
??????? // - Use the charset of template for the output
??????? // - Use text/html MIME-type
??????? response.setContentType("text/html; charset=" + t.getEncoding());
??????? Writer out = response.getWriter();

??????? // Merge the data-model and the template
??????? try {
??????????? t.process(root, out);

??????????? StringWriter stringWriter = new StringWriter();
??????????? t.process(root, stringWriter);
??????????? System.out.println(stringWriter.toString());
??????? } catch (TemplateException e) {
??????????? throw new ServletException(
??????????????????? "Error while processing FreeMarker template", e);
??????? }
??? }
}


模板文件中需要使用 ${}将java类中在Map对象中声明的变量包围起来就可以了

例如模板文件:


//描述信息是 : ${message}


则得到的结果是:


//描述信息是 : Hello World!的的的的

229284.html

礼物 2008-09-17 00:00 发表评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值